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.

Recommended Answers

All 4 Replies

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"?

num_value = atol (value);

num_value is an int
atol returns a long int

solution: long num_value;

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"?

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:

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!

Member Avatar for Siersan

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.

#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;
}

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>
#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;
}

Now the passkey can be any length that your memory can handle. The manual way of doing this is much harder.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.