Why does this loop keep going?

This is a discussion on Why does this loop keep going? within the C++ Programming forums, part of the General Programming Boards category; I have the following code: Code: #include <iostream> using namespace std; void get_int(int& number); int main() { int n; get_int(n); ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    197

    Why does this loop keep going?

    I have the following code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void get_int(int& number);
    
    int main()
    {
    int n;
    
    get_int(n);
    cout << "Final value read in = " << n << endl
    << "end of demo.\n";
    return 0;
    }
    
    
    void get_int(int& number)
    {
    char ans;
    do
    {
    cout << "Enter input number:";
    cin >> number;
    cout << "You entered " << number
    <<" Is that correct? (yes/no): ";
    cin >> ans;
    } while ((ans != 'Y') && (ans != 'y'));
    }
    If I enter a number, say 8, and then enter some random letters such as "gjh" the loop keeps going with out prompting me for more input. I can see why it would loop, but why does it not prompt me to enter another number?

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    20
    What compiler are you using ? When I compile it in Visual C++ 2005 Express, it works just fine.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    While ans does not equal 'Y'

    So when ans equals N it should quit...

    Hmm..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    >> if I enter a number, say 8, and then enter some random letters such as "gjh" the loop keeps going with out prompting me for more input. I can see why it would loop, but why does it not prompt me to enter another number?

    If you enter more then one character then they stay in the buffer. The leftover characters will be used for the next input of number.
    Kurt

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    cin >> number
    The first thing this line does is look in the buffer for data. It find nothing, so it blocks on input. You enter "8". >> consumes this completely and writes 8 into number. Then you come to this:
    cin >> ans
    You enter "ghj". >> consumes only the first character and puts it into ans. The rest remains in the buffer.
    'g' is neither 'Y' nor 'y'. The loop restarts.
    cin >> number
    >> looks again if there's something in the buffer. This time, it find "hj". It attempts to parse this as a number, but fails. It puts cin into a fail state and returns without changing number.
    cin >> ans
    >> sees that cin is in a fail state and returns immediately without affecting ans.
    ans is still 'g' and the game repeats.

    You therefore need to clear cin's input buffer at the start of the loop:
    Code:
    cin.ignore(cin.rdbuf()->in_avail(), '\n');
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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