c - Why is an int resetting to zero after each iteration of for loop? -
as title says, have simple loop cumulative variable += value of calculation. however, after each iteration, variable being reset zero. why?
here's loop:
int score_board(board* b) { int i; int total_score = 0; for(i = 0; < 4; i++) { printf("current total = %d\n",total_score); total_score += score_line(grab_line(b, i, 0)); total_score += score_line(grab_line(b, i, 1)); printf("scored row , columnn %d\tcum total=%d\n", i, total_score); } return total_score; }
and here's output i'm getting, proving while total_score set value want during 1 iteration, loses value upon resetting loop:
current total = 0 line score = 11000 line score = 40000 scored row , columnn 0 cum total=51000 current total = 0 line score = 60000 line score = 50000 scored row , columnn 1 cum total=110000 current total = 0 line score = 20000 line score = 40000 scored row , columnn 2 cum total=60000 current total = 0 line score = 50000 line score = 50000 scored row , columnn 3 cum total=100000
edit: found it! inside of score_line, int return wasn't initialized zero. rookie mistake.
thanks all.
[to "long" comment]
temporarily comment out calls score_line
, grab_line
, replace them integers...
total_score += 42; // score_line(grab_line(b, i, 0)); total_score += 42; // score_line(grab_line(b, i, 1));
and see if same results, if don't re-enable 1 of score_lines , test again, etc... sub-divide problem down until find offending function call.
Comments
Post a Comment