2

In the following program I ask a user to input a number which is greater than zero and from there the computer lists all numbers from 1 to that number.

For example, if the user entered 5, the program would output:

1
2
3
4
5

I'm trying to dummy proof the program so that if the user entered a letter for example, it would return an error and loop back to the prompt for the input again. For some reason this won't work though, and the console just continually outputs the error if the input is bad, forcing me to crash the program to quit it.

#include <iostream>

using namespace std;

int main()
{
    int x = 0;
    while (x <= 0)
    {
        //prompt user for input
        cout << "Please enter an integer greater than zero: ";
        cin >> x;

        //if input is not a number, or is less than or equal to zero, return an error
        //(this doesn't work if the input isn't a number)
        if (cin.fail())
        {
            cout << "Error: Please enter an integer." << endl;
            x = 0;
            cin.clear();
        }
        else if (x <= 0)
        {
            cout << "Error: Please enter an integer greater than zero." << endl;  
        }
    }

    //outout numbers between 1 and input (this is functioning)
    for (int i = 1; i <= x; i++)
    {
        cout << i << endl; 
    }

    //alert user and terminate program
    cout << "End of program. Exiting..." << endl;
    return 0;
}

Could someone please explain to me the best way to go about this? i.e. to catch the input and detect if it is invalid? I thought cin.bad() would do the trick, but I guess not?

Update: using cin.fail() still results in the looping of my error output. I also forgot to mention that I am relatively new to programming in C++, so I'd like to apologise if I don't understand something right away.

  • Using `cin.fail()` also results in the looping error. Is there another way to do this? –  Sep 24 '14 at 20:29
  • Apologies for the misconception about `stringstream`, I read over the post you linked me to. Perhaps a more accurate question would've been "How can I stop the looping error?" –  Sep 24 '14 at 20:35
  • The real point is the code in the `if(iss.fail())` body, that clears the streams failbits, and **consumes the wrong input**, to be able to proceed with the next input. It's perfectly OK to have a duplicate question here, BTW. – πάντα ῥεῖ Sep 24 '14 at 20:38
  • Oh thanks, I'm new to Stack Exchange/Overflow so I didn't know if duplicates were permitted. Would you mind explaining what a "stream failbit" is? I'm not sure I understand. –  Sep 24 '14 at 20:44
  • De nada! _"Would you mind explaining what a "stream failbit" is?"_ [Of course not ;-)](http://en.cppreference.com/w/cpp/io/basic_ios/fail). Also note regarding duplicate questions: The problem sometimes is, it may be hard to find them on SO. Nevertheless, take care of the proposed existing Q&A given to you, when you're typing in a question. [Read more about duplicates here please](http://stackoverflow.com/help/duplicates). – πάντα ῥεῖ Sep 24 '14 at 20:51
  • _@AnyDownvoters_ This question is valid and fits all policies stated in ['What topics can I ask about here?'](http://stackoverflow.com/help/on-topic). The appropriate answer can be found in the duplicate. No need to downvote, that question will be helpful in the actual state for future researchers. – πάντα ῥεῖ Sep 24 '14 at 23:24

0 Answers0