lundi 28 septembre 2015

Using cin.bad() to check for integer [duplicate]

This question already has an answer here:

If I type in letters it will just print out "Please enter a positive integer value: Please enter a positive integer value:", it does not allow me to reenter an input.

cout << "Please enter a positive integer value: ";
int num;
cin >> num ;

if(cin.bad() || num <= 0 ) {
    cout << "Please enter a positive integer value: ";
    cin >> num;
}

if(cin.bad() || num <= 0 ) {
    cout << "Please enter a positive integer value: ";
    cin >> num;
}

Aucun commentaire:

Enregistrer un commentaire