3

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
    The standard streams do not throw exceptions on failure by default. – T.C. Oct 03 '14 at 22:45
  • 1
    Check [`std::basic_ios::exceptions`](http://en.cppreference.com/w/cpp/io/basic_ios/exceptions), if you want to have exceptions thrown for specific stream failures. – πάντα ῥεῖ Oct 03 '14 at 22:45
  • 4
    `if (!( input % 1 ) || input == 0){ break; }` is just a long way to write `break;` – Ben Voigt Oct 03 '14 at 22:48
  • Also related: [How to test whether stringstream operator>> has parsed a bad type and skip it](http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it/26027067#26027067) – πάντα ῥεῖ Oct 03 '14 at 23:06

2 Answers2

6

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;
}
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thank you, I am used to Java. I have updated my question, my loop is still not breaking when it should? –  Oct 03 '14 at 23:04
  • 1
    @AdamJ: There is no meaningful test for whether `input` is an integer. No, really, its data type prohibits it from being anything else. Nuke the `if (!( input % 1 ) || input == 0)` or `if (( input / 1 != input ) || (input == 0))` or whatever you dreamt up. `input` is an integer. Even if the user types something that wasn't an integer, `input` is an integer -- and `cin` is marked as failed, which you know from `if (!cin)` or `while (!cin)` – Ben Voigt Oct 03 '14 at 23:09
1

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;
}
o11c
  • 15,265
  • 4
  • 50
  • 75