| | |
Access Violation (Segmentation Fault) + atol
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2005
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
char* value;
C++ Syntax (Toggle Plain Text)
value += int(passkey[x]);
C++ Syntax (Toggle Plain Text)
num_value = atol (value);
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
•
•
Join Date: Jan 2005
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Dave Sinkula
Where is the part that you have this pointer point to memory you can write to?C++ Syntax (Toggle Plain Text)
char* value;Can you describe in words what you believe this is doing?C++ Syntax (Toggle Plain Text)
value += int(passkey[x]);Since atol requires a null-terminated string, where do you null terminate the "string"?C++ Syntax (Toggle Plain Text)
num_value = atol (value);
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)
value += int(passkey[x]);
Am i completly wron in thinking this??? Please help.
Thanks for you help, dave!
•
•
•
•
Am i completly wron in thinking this???
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)
#include <iostream> using namespace std; int main() { char passkey[100]; int key = 0; cout<<"Enter a passkey: "; cin>> passkey; for (int i = 0; passkey[i] != '\0'; i++) key = 10 * key + int(passkey[i]); cout<<"The key value is "<< key <<endl; }
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main() { string passkey; int key = 0; cout<<"Enter a passkey: "; cin>> passkey; for (int i = 0; passkey[i] != '\0'; i++) key = 10 * key + int(passkey[i]); cout<<"The key value is "<< key <<endl; }
![]() |
Similar Threads
- Pointers as Map Key In Static Map Member (C++)
- access violation(segmentational fault) gah (C)
- program does not seems to run to end. (C++)
- help on 'Segmentation Fault' (C)
- Bug when creating linked lists in Dev C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: compile header file
- Next Thread: transfer input.txt to output.txt help prz
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






