Access Violation (Segmentation Fault) + atol

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 4
Reputation: cameronius is an unknown quantity at this point 
Solved Threads: 0
cameronius cameronius is offline Offline
Newbie Poster

Access Violation (Segmentation Fault) + atol

 
0
  #1
Jan 30th, 2005
Hello all,

i am new to C++ and im writing a little encryption program for fun. It requires the user to enter a 'passkey', which is a string (numbers, and letters). Then the program defines the ASCII value of all of the characters, and places them in the string - one after another. Then i want to use the "atol" function, to transfer the value of the string into the defined integer variable. This is where the program crashes and dispalys an error message "Access Violation (Segmentation Fault)".

Here is the code for that section:
--------------------------------

char* value;
int num_value;

cout << "\n" << "What would you like the PassKey to be? ";
cin >> passkey;

//**Convert to ASCII in this followiing Section**

int x = 0;

while (passkey[x] != '\0')
{
value += int(passkey[x]);
x++;
}

num_value = atol (value);


//**End of ASCII convert.**

-------------------------------------

The program crashes when it gets to "num_value = atol (value);". I am not sure what the problem is. Can you please help, im stuck.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Access Violation (Segmentation Fault) + atol

 
0
  #2
Jan 30th, 2005
  1. char* value;
Where is the part that you have this pointer point to memory you can write to?
  1. value += int(passkey[x]);
Can you describe in words what you believe this is doing?
  1. num_value = atol (value);
Since atol requires a null-terminated string, where do you null terminate the "string"?
Last edited by Dave Sinkula; Jan 30th, 2005 at 12:34 am. Reason: Added third code fragment and question.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 2
Reputation: picogeek is an unknown quantity at this point 
Solved Threads: 0
picogeek picogeek is offline Offline
Newbie Poster

Re: Access Violation (Segmentation Fault) + atol

 
0
  #3
Jan 30th, 2005
num_value = atol (value);

num_value is an int
atol returns a long int

solution: long num_value;
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 4
Reputation: cameronius is an unknown quantity at this point 
Solved Threads: 0
cameronius cameronius is offline Offline
Newbie Poster

Re: Access Violation (Segmentation Fault) + atol

 
0
  #4
Jan 30th, 2005
Originally Posted by Dave Sinkula
  1. char* value;
Where is the part that you have this pointer point to memory you can write to?
  1. value += int(passkey[x]);
Can you describe in words what you believe this is doing?
  1. num_value = atol (value);
Since atol requires a null-terminated string, where do you null terminate the "string"?
Dave,

Thankyou for replying, as i stated i am new to this, and am not really sure of what i am doing.

I dont understand the first question you asked. Im confused, can you explain, and i might be able to help you.

Second - well, this while block takes each individual character of the string, and converts it to ASCII. The part that states:
  1. value += int(passkey[x]);
I believe that this takes the selected character of the string, in ASCII form, and adds it to the string. eg. say the string was = "12658" and the value of the character i was adding was 10, the string would = "1265810"

Am i completly wron in thinking this??? Please help.

Thanks for you help, dave!
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 45
Reputation: Siersan is an unknown quantity at this point 
Solved Threads: 2
Siersan's Avatar
Siersan Siersan is offline Offline
Speaker of Truth

Re: Access Violation (Segmentation Fault) + atol

 
0
  #5
Jan 30th, 2005
Am i completly wron in thinking this???
It doesn't work like that. Strings represented by character arrays are very low level. You have to do everything manually, including allocate enough memory and terminate the string with '\0'.

The way you're doing it, you really want to avoid appending characters to a string because the ASCII values you're working with are all double digits. Try this instead.
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char passkey[100];
  8. int key = 0;
  9.  
  10. cout<<"Enter a passkey: ";
  11. cin>> passkey;
  12.  
  13. for (int i = 0; passkey[i] != '\0'; i++)
  14. key = 10 * key + int(passkey[i]);
  15.  
  16. cout<<"The key value is "<< key <<endl;
  17. }
The problem with this is that you're limiting a user to 99 characters for the passkey. Since this is C++, you should take advantage of the string class.
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string passkey;
  9. int key = 0;
  10.  
  11. cout<<"Enter a passkey: ";
  12. cin>> passkey;
  13.  
  14. for (int i = 0; passkey[i] != '\0'; i++)
  15. key = 10 * key + int(passkey[i]);
  16.  
  17. cout<<"The key value is "<< key <<endl;
  18. }
Now the passkey can be any length that your memory can handle. The manual way of doing this is much harder.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC