I have a little problem with integer and array.
I want to receive a values from user and split it to array in one line.
For example,the value is 7890 (receive it in one line)
and then put it in array like this a[0]=7,a[1]=8,a[2]=9,a[3]=0 .
1 - get the input in a string
2 - create an array in the free store or use a container using the length of that string as size
3 - convert each character of the string in an item for the array
Could you read in a string, use the STL reverse method on the string, and then just use the string to access each digit, performing the conversion from ASCII char to numeric char after retrieval?
Here's what I meant; it will supplement what jsmith and Bazzy contributed:
1 2 3 4 5 6
#include <algorithm>
string number = "7890";
reverse( number.begin(), number.end() );
// number would now be "0987" so that the subscripts would match up as you requested
Bazzy (192) Nov 14, 2008 at 10:27pm
1 - get the input in a string
2 - create an array in the free store or use a container using the length of that string as size
3 - convert each character of the string in an item for the array
Thank you but
Could you please show me at step 3 convert each character of the string in an item for the array. Thank a lot.
To get a character from a string, you can use sub-scripting: mystring[n] returns the character at positin n in mystring (notice that the first character will have position 0).