3

I have a string that contains what ever the user has input

string userstr = "";
cout << "Please enter a string ";
getline (cin, userstr);

The string is then stored in userstr, I then want the string to be stored in a integer array where each character is a different element in the array. I have created a dynamic array as the following:

int* myarray = new int[sizeof(userstr)]; 

However how do I then get my string into that array?

2
  • 2
    sizeof(userstr) is not the same as userstr.length() or userstr.size(). Commented Dec 8, 2013 at 20:51
  • possible duplicate of C++ String Length? Commented Dec 8, 2013 at 20:57

4 Answers 4

6

You can access each element in your string using the [] operator, which will return a reference to a char. You can then deduct the int value for char '0' and you will get the correct int representation.

for(int i=0;i<userstr.length();i++){
    myarray[i] = userstr[i] - '0';
}
Sign up to request clarification or add additional context in comments.

4 Comments

when i do this and display myarray i just get random numbers: for example if i enter "hey my name is" the out put is 006B4D30
I am assuming what is entered is 'supposed' to be an int. If you want to store a string as an array of integers then what you are doing is storing the integer representing each char - is this what you are trying to do? I am unclear as to the purpose if that is the case
sorry yes that is what i am trying to do, its been a very long day o.O
In that case you can just remove the " - '0' " from the code. Then, when you wish to convert it back to a string, you have to cast each integer back to a char. For example, if you want to output the first character from the string stored as an int you just say cout << (char) userstr[0];
3
int* myarray = new int[ userstr.size() ];

std::copy( usestr.begin(), userstr.end(), myarray ); 

The terminating zero was not appended to the array. If you need it you should allocate the array having one more element and place the terminating zero yourself.

5 Comments

when i do this i get this error: Windows has triggered a breakpoint in cw2.exe. This may be due to a corruption of the heap, which indicates a bug in cw2.exe or any of the DLLs it has loaded. This may also be due to the user pressing F12 while cw2.exe has focus. The output window may have more diagnostic information.
+1. Naturally, one could also declare myarray in a way that would automatically free its memory when it goes out of scope. I.e int myarray[ userstr.size() ];
but userstr is not a constant, it gives me an error saying expression must have a constant value
Oh, it seems I have understood the reason of the error. You should allocate the array dynamically. It seems that you are trying to declare it in stack or statically.
oh i thought this : --> int* myarray = new int[ userstr.size() ]; allocates the array dynamically?
1

You can just simply use isstringstream to convert the string to int as follows

istringstream istringName(intString);
istringName >> real_int_val;

now it has magically become a int containing all numbers from string However I do not see why you would not cin it as a int in the first place??

Comments

-1

Here is one way to do it

for(int i=0;i<userstr.length();i++){
    myarray[i] = userstr[i];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.