![]() |
| ||
| Access Violation (Segmentation Fault) + atol 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. |
| ||
| Re: Access Violation (Segmentation Fault) + atol char* value;Where is the part that you have this pointer point to memory you can write to? value += int(passkey[x]);Can you describe in words what you believe this is doing? num_value = atol (value);Since atol requires a null-terminated string, where do you null terminate the "string"? |
| ||
| Re: Access Violation (Segmentation Fault) + atol num_value = atol (value); num_value is an int atol returns a long int solution: long num_value; |
| ||
| Re: Access Violation (Segmentation Fault) + atol Quote:
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: 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! |
| ||
| Re: Access Violation (Segmentation Fault) + atol Quote:
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. #include <iostream>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. #include <iostream>Now the passkey can be any length that your memory can handle. The manual way of doing this is much harder. |
| All times are GMT -4. The time now is 7:48 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC