質問

I am trying to read a string of 9 characters into 9 integer values, to be stored in an array (for now I store them in 9 separate ints, will put them in the array once they read in OK). General approach i took: con the string, split it into 9 character values, convert (atoi) each into an integer and store as 9 ints, put the ints into the array. What is strange is, while the single values get split into the single characters with no problems, atop somehow "sees" the other neighboring values (not contained in that character at all!) and converts them backwards.

sample code:

countrows = 1;
countcols = 1;
cout << endl << "Enter values for boxes in row " << countrows << ", enter 0 for open boxes (enter 9 numbers, with no spaces or delimiters): ";
string inputline;
cin >> inputline;
char col1, col2, col3, col4, col5, col6, col7, col8, col9;
int int1, int2, int3, int4, int5, int6, int7, int8, int9;
col1 = inputline[0];
col2 = inputline[1];
col3 = inputline[2];
col4 = inputline[3];
col5 = inputline[4];
col6 = inputline[5];
col7 = inputline[6];
col8 = inputline[7];
col9 = inputline[8];
int1 = atoi(&col1);
int2 = atoi(&col2);
int3 = atoi(&col3);
int4 = atoi(&col4);
int5 = atoi(&col5);
int6 = atoi(&col6);
int7 = atoi(&col7);
int8 = atoi(&col8);
int9 = atoi(&col9);
cout << "inputline: " << inputline << endl;
cout << "col1: " << col1 << " col2: " << col2 << " col3: " << col3 << endl; //debug line
cout << "int1: " << int1 << " int2: " << int2 << " int3: " << int3 << endl; //debug line

the result of this is:

Enter values for boxes in row 1, enter 0 for open boxes (enter 9 numbers, with no spaces or delimiters): 456123789 inputline: 456123789 col1: 4 col2: 5 col3: 6 int1: 4 int2: 54 int3: 654

why does int contain 5 and int3 65 (should be int1: 4 int2: 5 int3: 6)

役に立ちましたか?

解決

atoi is used for converting NULL-terminated strings (char*), and not characters into integer. Your col1, col2, are not NULL terminated strings, and the atoi will read the memory until it gets to the NULL value (which will end the string).

If you want to change ASCII digits into numbers, you could use simple math:

int1 = col1 - '0';

col1: 4 col2: 5 col3: 6 int1: 4 int2: 54 int3: 654

All these values are stored on stack. Let's assume that stack is currently empty (it isn't, there are other locals, return address, and such things), and that top element is 0:

 STACK
 ----------
 0           <- top

Now, your col1, col2, col3 are placed on the stack, as you declare them:

 STACK
 ----------
 0
 col1
 col2
 col3

And, once you assing them values, you will get following picture:

 STACK
 ----------
 0
 '4'
 '5'
 '6'

When you call atoi(col1) it will read '4', and then, 0, which will terminate string, and it will parse only ASCII '4'. When you call atoi(col2), it will read '5', '4', and then 0, so input string will be "54" so it will parse exactly that. Similar will hapen for all other col variables.

Note that there is nothing magical about reading stack elements in the reverse order - actually, you are reading memory in the direct order - because on my (and probably yours) machine stack is growing downward. On some machines this would not be the case (check this link for more details), and you would get 456... for col1 or probably just zero (atoi will return zero if you pass non-number string as the argument).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top