Verifying int with while(!(cin >> num))

This is a discussion on Verifying int with while(!(cin >> num)) within the C++ Programming forums, part of the General Programming Boards category; Ok....this code Code: #include <iostream> #include <limits> using namespace std; int main() { int f1; cout << "Enter a number: ...

  1. #1
    Registered User motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14

    Verifying int with while(!(cin >> num))

    Ok....this code

    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main()
    {
         int f1;
    
         cout << "Enter a number: ";
         while(!(cin >> f1) || cin.get() != '\n')
         {
              cin.clear();
              cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
              cout << "Error, invalid input.\nPlease re-enter: ";
         }
    }
    works fine by itself (Thanks Daved), but when I implement it into my program it makes me hit enter twice when I input any integer. Aside from making me hit enter twice, the program executes like it should. Below is the code, with most of the functions taken out of it.
    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    void VerifyInteger(int &);
    inline void ClearCin();
    
    int main()
    {
    
         int Selection;
         bool TryAgain;
    
         cout << endl << "1. Choice One" << endl;
         cout << endl << "2. Choice Two" << endl;
         cout << endl << "3. Choice Three" << endl;
         cout << endl << "Please enter a selection: ";
    
         do
         {
    
              VerifyInteger(Selection);
    
              if(Selection > 0 && Selection < 4)
              {
                   TryAgain = false;
                   switch(Selection)
                   {
                             case 1:
                                  // Perform first function
                                  break;
                             case 2:
                                  // Perform second function
                                  break;
                             case 3:
                                  // Perform third function
                                  break;                    
                   }
              }
              else
              {
              ClearCin();
              cout << "Error\nPlease re-enter a valid selection: ";
              TryAgain = true;
              }
         }while(TryAgain == True);
    
    return 0;
    }
      
    void VerifyInteger(int &num)
    {
         while(!(cin >> num) || cin.get() != '\n')
         {
              ClearCin();
              cout << "Error\nPlease re-enter a valid selection: ";
         }
    }
    
    inline void ClearCin()
    {
         cin.clear();
         cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
    }
    Can anyone help me out with why this is happening?
    Last edited by motarded; 02-26-2006 at 04:14 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    It's like this:

    You enter the digit 5 and into the buffer goes 5 and then the '\n'
    Code:
    5\n
    Your validation statement first uses an extraction that takes the 5 and returns true, your buffer now looks like this:
    Code:
    \n
    Now your cin.get() takes the newline and now the buffer is empty. Back in main, it goes into your if statement of being between 0-4, it isn't. It goes to the else and you call ClearCin(). Why? Cin hasn't failed.

    Now in ClearCin() you call cin.ignore(), which is meant to ignore the next character in the stream, but there is nothing in the stream, so you have to put something in there for it to ignore. Hence, the second enter. Remove that ClearCin() from the else, you simply don't need it.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    but when I implement it into my program it makes me hit enter twice when I input any integer.
    First, your code won't even compile. Second, when I enter 1, it works fine.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    With my code example you only want to "clear cin" on invalid input. If your code is setup so that you'd prefer to "clear cin" after any input, valid or invalid, then you could change get() to peek() so that the newline is left in the stream and will be caught by the call to ignore() when ClearCin() is called.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 11:28 PM
  2. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM

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