c - Why not incrementing z? -


i trying write simple state machine executor in c. have code:

while(1) {     strcpy(fsm.state[x][z], lines[i]);   printf("%i %i\n", x, z);   z++; i++;   if(strcmp(lines[i], ".") == 0) x++; z = 0;   if(strcmp(lines[i], "") == 0) break; } 

i don't understand why if z should reset when current line reading lines array equal ".", happens every third occurrence in test scenario, z stays equal 0, when x incremented every third line.

i need output so:

1 0 \n 1 1 \n 1 2 \n 2 0 \n 2 1 \n 2 2 \n 3 0 \n 3 1 , etc...

instead get:

1 0 \n 1 0 \n 1 0 \n 2 0 \n 2 0 \n 2 0 \n 3 0 \n 3 0, etc...

what have change? might stupid question, don't understand what's wrong here.

thank kindly help.

z being set zero. if statement doesn't work based on line, goes next semi-colon.

if(strcmp(lines[i], ".") == 0) x++; z = 0; if(strcmp(lines[i], "") == 0) break; 

is same as:

if(strcmp(lines[i], ".") == 0){  x++; }  z = 0;  if(strcmp(lines[i], "") == 0){  break; } 

since have 2 statements (x++; z = 0;), need put braces around them specify condition:

if(strcmp(lines[i], ".") == 0){  x++;  z = 0; } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -