944,120 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7787
  • C++ RSS
Jan 30th, 2005
0

Access Violation (Segmentation Fault) + atol

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cameronius is offline Offline
4 posts
since Jan 2005
Jan 30th, 2005
0

Re: Access Violation (Segmentation Fault) + atol

C++ Syntax (Toggle Plain Text)
  1. char* value;
Where is the part that you have this pointer point to memory you can write to?
C++ Syntax (Toggle Plain Text)
  1. value += int(passkey[x]);
Can you describe in words what you believe this is doing?
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jan 30th, 2005
0

Re: Access Violation (Segmentation Fault) + atol

num_value = atol (value);

num_value is an int
atol returns a long int

solution: long num_value;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
picogeek is offline Offline
2 posts
since Jan 2005
Jan 30th, 2005
0

Re: Access Violation (Segmentation Fault) + atol

Quote originally posted by Dave Sinkula ...
C++ Syntax (Toggle Plain Text)
  1. char* value;
Where is the part that you have this pointer point to memory you can write to?
C++ Syntax (Toggle Plain Text)
  1. value += int(passkey[x]);
Can you describe in words what you believe this is doing?
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cameronius is offline Offline
4 posts
since Jan 2005
Jan 30th, 2005
0

Re: Access Violation (Segmentation Fault) + atol

Quote ...
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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 12
Solved Threads: 2
Light Poster
Siersan is offline Offline
45 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: compile header file
Next Thread in C++ Forum Timeline: transfer input.txt to output.txt help prz





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC