Thread: while loop not resetting

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    3

    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;}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,158
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding Resetting password
    By achardeepak in forum C Programming
    Replies: 2
    Last Post: 02-20-2017, 10:13 PM
  2. Regarding Resetting password
    By achardeepak in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2017, 10:13 PM
  3. Resetting std:vector?
    By DrSnuggles in forum C++ Programming
    Replies: 7
    Last Post: 09-06-2008, 03:53 PM
  4. Resetting stdin after EOF
    By o0zi in forum C Programming
    Replies: 4
    Last Post: 05-22-2005, 08:04 AM
  5. Resetting the WM_TIMER
    By conright in forum Windows Programming
    Replies: 1
    Last Post: 01-04-2003, 12:02 AM

Tags for this Thread