Ending after do while loop

This is a discussion on Ending after do while loop within the C++ Programming forums, part of the General Programming Boards category; Hi, I've made a guess my number program and it ends straight after the while loop is met. And passes ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    Ending after do while loop

    Hi, I've made a guess my number program and it ends straight after the while loop is met. And passes my cout thing after.

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        int guess;
        srand(time(0));
        int theNumber = rand() % 100 + 1;
        int tries = 0;
        
        cout << "Welcome to Ben's Guess My Number game!" << endl;
        
        do
        {
            cout << "Guess a number in between 0 and 100." << endl;
            cout << "Number: ";
            cin  >> guess;
            tries++;
            
            if (guess < theNumber)
            
                      cout << "Too low." << endl;
            
            
            if (guess > theNumber)
            
                      cout << "Too high." << endl;
            
            
        } while (guess != theNumber);
        
        cout << "Congratulations you guessed the number in " << tries << ", well done!" << endl;
        
        cout << "Press the return key to proceed...";
        cin.get();
        return 0;
            
        
        
        
    }
    Thanks ,
    Ben

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    The cin >> guess reads in the number but leaves the new line on the buffer. Your cin.get() later reads in that new line, and thus you do not get the pausing effect that you want. A solution is to have a cin.ignore() just before the cin.get() so as to ignore that extra character. If you want to be on the safe side (e.g., to discard trailing spaces after the number) you could #include <limits> and use:
    Code:
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cin.get();
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    Thanks

    Thanks, it worked. But i'm still pretty confused. Whats the definition of buffer? What do you mean read in new line. Thanks again.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Whats the definition of buffer?
    I will let the experts fill you in on the definition, but I just think of it as a temporary holding location for input (or output, in the case of buffered output) before it is processed.

    What do you mean read in new line.
    The enter you press to enter the input puts in a new line sequence to the buffer. It is basically the same as in a text file: each line has an invisible marker (i.e., the new line sequence, line ending, or whatever you want to call it) to denote the end of the line. It is typically one of '\r', '\n', or "\r\n" depending on your system.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    So...

    So, the cin places the input into the buffer, and when I use cin.get it reads from buffer and thinks somethings been pressed? Or am I way off lol.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    So, the cin places the input into the buffer, and when I use cin.get it reads from buffer and thinks somethings been pressed?
    No, your entering of input places the input into the buffer. The use of cin places the data from the buffer into your variables.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can use cin in different ways. You can use get(), ignore(), operator>>, getline, and other functions.

    As laserlight said, when the user of your program types at the console, the console waits until the user hits enter. Then, all the characters the user typed, including a hidden newline character, are stored in the buffer.

    When your program uses cin to read, it checks the buffer for data it needs. For example, if you use operator >> to read into an integer, cin checks the buffer for any data. At first, the buffer is empty, so the console waits for the user to type something and hit enter. Then cin sees the number the user typed and reads it in, then stops. The newline character is still in the buffer.

    If you use operator>> again, it skips over whitespace characters automatically. That means the newline from the previous read is skipped, so you don't notice it. However, cin.get() reads differently. It gets a single character no matter what, meaning it does not skip over whitespace. At the end of your program, there is still a newline character in the buffer. The call to get() gets that newline character and returns immediately, so your window doesn't stay open.

    If you call ignore(), it ignores the data in the buffer, which in this case is a newline. That leaves the buffer empty. Then you call cin.get() to get a character, but because the buffer is empty the console waits for the user to type in something and hit enter. While it is waiting the console window stays open so you can see the output, and then you hit enter and it closes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM

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