javs
javs

Reputation: 27

Verifying User Input as Integer: User has to enter input twice

I am prompting the user to enter an integer value. When the value is incorrect, the program works. However, when the user enters an integer input, the user needs to enter the input twice.

I looked at other tutorials on how to use the while loop to catch erroneous input, and that part worked for me. However, the integer values need to be entered twice in order for the program to run.

#include <iostream>
using namespace std;

int main() {
    cout << "*************************************************" << endl;
    cout << "******************|DVD Library|******************" << endl;
    cout << "*************************************************" << endl;
    cout << "1.\tAdd DVD" << endl;
    cout << "2.\tDelete DVD" << endl;
    cout << "3.\tSearch DVD" << endl;
    cout << "4.\tList All DVDs in the Library" << endl;
    cout << "5.\tAdd DVD to Favorites List" << endl;
    cout << "6.\tDelete DVD from Favorites List" << endl;
    cout << "7.\tSearch DVD in Favorites List" << endl;
    cout << "8.\tList All DVDs in Favorites List" << endl;
    cout << "9.\tQuit" << endl;
    cout << "*************************************************" << endl;

    int input;
    cin >> input;
    while (!(cin >> input)) {
        cin.clear();
        while (cin.get() != '\n')
            continue;
        cout << "Please enter an integer --> " << flush;
    }
    if (input < 1 || input > 9) {
        cout << "Invalid input! Please try again!" << endl;
    }
    return 0;
}

Upvotes: 0

Views: 88

Answers (4)

javs
javs

Reputation: 27

Thanks everyone! The "cin >> input;" line was unnecessary. At first, I left it there because it would actually tell the user the error message if the user entered a numeric input such as a double. So, if the user entered something like 3.3, the program would display an error message that I specified ("Please enter an integer" line). However, the program in this case (when there is a double) asks the user to prompt for the integer input twice and then continues the program. When I delete the said unnecessary line, the program accepts a double input, but what it does, it takes the numeric value before the decimal point and uses it as the integer. So, a value of 1.2 is recorded as 1 when I tested it. I'm unsure why this phenomenon happens, but the program works otherwise. Maybe it accounts for human error?

#include <iostream>
using namespace std;

int main() {
    cout << "*************************************************" << endl;
    cout << "******************|DVD Library|******************" << endl;
    cout << "*************************************************" << endl;
    cout << "1.\tAdd DVD" << endl;
    cout << "2.\tDelete DVD" << endl;
    cout << "3.\tSearch DVD" << endl;
    cout << "4.\tList All DVDs in the Library" << endl;
    cout << "5.\tAdd DVD to Favorites List" << endl;
    cout << "6.\tDelete DVD from Favorites List" << endl;
    cout << "7.\tSearch DVD in Favorites List" << endl;
    cout << "8.\tList All DVDs in Favorites List" << endl;
    cout << "9.\tQuit" << endl;
    cout << "*************************************************" << endl;

    int input;
    while (!(cin >> input)) {
        cin.clear();
        while (cin.get() != '\n')
            continue;
        cout << "Please enter an integer --> " << flush;
    }
    if (input < 1 || input > 9) {
    cout << "Invalid input! Please try again!" << endl;
}
return 0;

}

Upvotes: 0

Swordfish
Swordfish

Reputation: 13144

int input;
while (cout << "Your choice: ",
       !(cin >> input) || input < 1 || 9 < input)
{
    cin.clear();
    while (cin.get() != '\n');
    cerr << "Invalid input! Please try again!\n";
}

Upvotes: 1

john
john

Reputation: 88017

'The user has to enter the input twice' Look at your code

int input;
cin >> input;
while(!(cin >> input )){

How many times do you ask the user for input?

You'd have more luck with this

int input;
while(!(cin >> input )){

Your error recovery code looks reasonable, haven't tested it though.

Upvotes: 4

wilx
wilx

Reputation: 18268

You ask for the input twice:

cin >> input;
while(!(cin >> input )){

Removing the first line might make it work you intended.

Upvotes: 4

Related Questions