std::cin doesn't throw an exception on bad inp

2020-02-13 06:20发布

问题:

I am just trying to write a simple program that reads from cin, then validates that the input is an integer. If it does, I will break out of my while loop. If not, I will ask the user for input again.

My program compiles and runs just fine, which is great. But it doesn't prompt for new input if I enter a non numeric value. What gives?

#include <iostream>
using namespace std;

int main() {
    bool flag = true;
    int input;
    while(flag){
        try{ 
            cout << "Please enter an integral value \n";
            cin >> input;
            if (!( input % 1 ) || input == 0){ break; }
        }
        catch (exception& e)
        { cout << "Please enter an integral value"; 
        flag = true;}
    }
    cout << input;
    return 0;
}

回答1:

C++ iostreams don't use exceptions unless you tell them to, with cin.exceptions( /* conditions for exception */ ).

But your code flow is more natural without the exception. Just do if (!(cin >> input)), etc.

Also remember to clear the failure bit before trying again.

The whole thing can be:

int main()
{
    int input;
    do {
       cout << "Please enter an integral value \n";
       cin.clear();
       cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    } while(!(cin >> input));
    cout << input;
    return 0;
}


回答2:

Don't use using namespace std; Instead import what you need.

It's better to do input a line at a time. This makes behavior much more intuitive if you have multiple words on one line, or if you press enter before typing anything.

#include <iostream>
#include <sstream>
#include <string>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::flush;
using std::getline;
using std::istringstream;
using std::string;

int main() {
    int input;
    while (true)
    {
        cout << "Please enter an integral value: " << flush;
        string line;
        if (!getline(cin, line)) {
            cerr << "input failed" << endl;
            return 1;
        }
        istringstream line_stream(line);
        char extra;
        if (line_stream >> input && !(line_stream >> extra))
            break;
    }
    cout << input << endl;
    return 0;
}