Do/while loop error - C prg

This is a discussion on Do/while loop error - C prg within the C Programming forums, part of the General Programming Boards category; I have two do/while loops in my program. One is stopping as expected and waiting for a reply from the ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Springfield, IL
    Posts
    5

    Do/while loop error - C prg

    I have two do/while loops in my program. One is stopping as expected and waiting for a reply from the keyboard input. In the second, it runs through the loop twice each time and I can't figure out why. Here is the code for the failing loop.

    Code:
        }else{        
            char result = ' ';
                
            do{        
            
                randnum = genRandNum(lower,upper);    //generating random number between range
                printf ("I will now guess a number between %d and %d, which is: %d.\n", lower, upper, randnum);
                
                printf ("Is my guess (c)orrect, (l)ow, or (h)igh?\n");
                scanf ("%c", &result);
                
                guesscount++;
                
                if (result =='l'){
                
                    if (randnum > lower && randnum < upper){    //making sure lower range limit should be changed
                        lower = randnum;
                    }
                    
                }else{
                    
                    if (result == 'h'){
                    
                        if (randnum > lower && randnum < upper){    //making sure upper range limit should be changed
                            upper = randnum;
                        }
                    }else{
                    
                        printf ("Great, I got it right in %d guesses.\n",guesscount);
                    }
                        
                }
            }while (result != 'c');

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Change this:
    Code:
    scanf ("%c", &result);
    To this:
    Code:
    scanf (" %c", &result);
    The first time the scanf function is called it will read the user's h/l/c character but leave the newline character in the buffer. The second time through the loop, the scanf call reads that leftover newline in the buffer. Then, the third call will read in a valid h/l/c but again leave the newline. The end result is that every other time through the loop, your scanf call will consume a leftover newline instead of a valid character. The space in front of the %c format specifier takes care of this leftover newline for you.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Springfield, IL
    Posts
    5
    Thank you. I have been beating myself up over this. The first loop is working with an integer, so that is why it didn't fail.

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Change line 29 to
    Code:
                    printf ("Great, I got it right in %d guesses.\nresult was %d\n",guesscount, result);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-22-2012, 12:50 AM
  2. error (do-while)loop
    By prehisto in forum C Programming
    Replies: 8
    Last Post: 10-30-2011, 10:06 AM
  3. Do While Loop Error
    By mintsmike in forum C Programming
    Replies: 4
    Last Post: 03-24-2009, 03:45 PM
  4. error with my for loop?
    By rs07 in forum C Programming
    Replies: 1
    Last Post: 07-25-2008, 08:12 PM
  5. error in a for loop
    By saahmed in forum C Programming
    Replies: 11
    Last Post: 03-10-2006, 12:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21