Search code examples
c++getcingetline

How to clear line of input, not just single character


I'm sorry if this is a simple question, I am a beginner. I want to be able to clear the input from cin if it is not the expected type. I have it working for a single character or value, but the problem arises when I enter more than one character on the line.

For example, the user is prompted for a double. If it is not a double I get an error message and reprompt. This should also occur if I enter a longer string.

EX 1: Expected output

Enter initial estimate: a

The initial estimate is not a number.
Enter initial estimate: afdf

The initial estimate is not a number. 

EX 2: In my code currently, the afdf is continually read so I get:

Enter initial estimate of root : a

The initial estimate was not a number
Enter initial estimate of root : afdf

The initial estimate was not a number
Enter initial estimate of root :
The initial estimate was not a number
Enter initial estimate of root :
The initial estimate was not a number
Enter increment for estimate of root :
The increment was not a number

I have tried using cin.clear() and cin.get() as well as looked into getline() but this did not work.

 while (numTries < 4)
 {
   numTries++;
   cout << "Enter initial estimate of root : ";
   cin >> estimate;

   if (!(cin.fail()))
   {
     if ((estimate >= minEst) && (estimate <= maxEst))
     {
       break;
     }
     else
     {
       if (numTries == 4)
       {
         cout << "ERROR: Exceeded max number of tries entering data" << endl;
         return 0;
       }
       cout << "" << endl;
       cout << "Value you entered was not in range\n";
       cout << fixed << setprecision(3) << minEst << " <= initial estimate <= " << maxEst << endl;
     }
   }
   else
   {
   cout << "\nThe initial estimate was not a number\n";
   cin.clear();
   cin.get();
   }
 }

How can I make sure the input is cleared for the next time it is to be entered? Can I use getline() to achieve this? Thanks in advance.


Solution

  • If you want to stick with using cin then you will want to ignore the rest of the line with cin.ignore()

    #include<limit>
    ...
    
    double estimate;
    do {
        if(cin.fail()) {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "The initial estimate was not a number" << endl;
        }
        cout << "Enter initial estimate of root: ";
        cin >> estimate;
        cout << endl;
    } while(!cin);
    

    Getline may be a better option since it gets a line from an input stream delimited by the newline character (\n).

    do {
        if(cin.fail()) {
            cin.clear();
            cout << "The initial estimate was not a number" << endl;
        }
        cout << "Enter initial estimate of root: ";
    } while(!getline(cin, estimate);