arrays - Loop in C gone wrong -
i have code
for (i = 0; < s; i++) // 0 runners { (j = 0; j < 4; j++) //for j 0 laps { printf("\nenter time of lap %d runner %d in minutes: ", j+1, i+1); // prompt time each runner in minutes while (scanf("%d", &runnerm[i][j]) != 1) // while scanf returns not equal 1 (value parsed == true) { while ((ch = getchar()) != '\n') putchar(ch); // check if ch=character, compare last entry (\n if character) printf(" not integer.\nplease enter "); // print error printf("integer, such 1, 5, or 9 : \n"); // cnt print error } printf("enter time of lap %d runner %d in seconds: ", j+1, i+1); //prompt time each runner in seconds while (scanf("%d", &runners[i][j]) != 1) // while scanf returns not equal 1 (value parsed == true) { while ((ch = getchar()) != '\n') putchar(ch); // check if ch=character, compare last entry (\n if character) printf(" not integer.\nplease enter "); // print error printf("integer, such 1, 5, or 9 : \n"); // cnt print error } printf(" \n check 1 \n "); printf("\n %d minutes -- %d seconds \n", runnerm[i][j], runners[i][j]); // correct check! printf(" \n check 1.5 \n "); printf("\n %d minutes -- %d seconds \n", runnerm[i][0], runners[i][0]); // incorrect check! printf("\n %d minutes -- %d seconds \n", runnerm[i][1], runners[i][1]); // incorrect check! printf("\n %d minutes -- %d seconds \n", runnerm[i][2], runners[i][2]); // incorrect check! printf("\n %d minutes -- %d seconds \n", runnerm[i][3], runners[i][3]); // incorrect check! } }
the problem is, when check 1.5 runs second time, runnerm[i][j]
gets values runners[i+1][j]
in short, when changes value, previous stored value of runnerm
value of runners being stored..
why that? can't find reason..
edit: "check 1" , "check 1.5" exist whole purpose of -checking- values @ given time of loop. so, if delete them, program still broken. when fixed, checks deleted, because won't need check values more. thank answering don't why suggest checks, there debugging. :\
edit found caused problem, when used version of program used fewer arrays, problem solved, when started adding random un-used arrays, problem appears. why that? (new problem) memory overlaps 1 runners[i][j] use?
it appears printf
statement showing next runner/next time, recording runner/time before:
printf("\nenter time of lap %d runner %d in minutes: ", j+1, i+1); ^^^^ ^^^^
why not i
j
Comments
Post a Comment