-
while loop not resetting
hello everyone,
I've tried to construct a nested while loop and for the nested loop the value output isn't returning to 0 at the beginning of the new loop. How might I fix this. Also, the final return value of successful vs unsuccessful simulations is wrong but also inconsistent. I would appreciate any help offered to solve either problem.
Code:
int main(){
int flag = 1;
int days, failure,run=0, success, adjustments, simulations, trial=0;
printf("How many days did we use the simulator?\n");
scanf("%d", &days);
while (flag){
(trial++);
printf("How many simulations did we run on day %d?\n", trial);
scanf("%d", &simulations);
int tag=1;
while (tag){
(run++);
printf("How many adjustments were needed for run #%d?\n", run);
scanf("%d", &adjustments);
if (run == simulations)
tag =0;}
if (adjustments >= 5)
failure=run;
printf("Day #%d: There were %d successful simulations.\n", trial, success);
printf("\n");
if (trial == days)
flag = 0;}
-
I'd start by indenting your code consistently: every time control enters the body of a function, if statement, loop, indent by one level. Every it exits the body of that construct, decrease the indent by one level. This makes it easier to see the flow of control through your code, which can be helpful for this kind of debugging.
It can also be helpful to place closing braces on their own line (with the possible exceptions of if statements with a matching else, and do while loops).