I want from program that it force the user to input valid value.When ever user enter caharcter in integer valriable it force the user to input the right value.But this does not work.If user input invalid value it start looping.

#include <iostream>

using namespace std;

int main()
{
    int x;
    re_enter_x:
    cout<<"Enter X";
    cin>>x;
    if(cin.fail())
    {
        cout<<"INvalid Input ";
        cin.clear();
        cout<<x<<endl;
        goto re_enter_x;
    }
    else
    {
        cout<<"Valid Number ";
    }
    return 0;
}

Recommended Answers

All 3 Replies

Avoid goto. Forget it exists. Try loops that don't exit till the input is right.

commented: Same result when I use any Loop.Anyway solved problem by using cin.ignore(); +0

But this does not work.If user input invalid value it start looping.

Indeed. When cin fails to make a conversion, it doesn't remove the invalid characters from the stream. So you have two options:

  1. "Flush" the stream (not recommended).
  2. Read a full line of data and then parse it yourself such as with a stringstream so that cin remains clean naturally.

The second option is recommended because it gives you more flexibility, and "flushing" streams can be tricky. However, the flexibility means you want to choose from numerous methods of going about it. I'll get you started with a simple one:

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

using namespace std;

namespace
{
    template <typename T>
    bool parse_int(string source, T& value)
    {
        istringstream iss(source);

        return !!(iss >> value);
    }
}

int main()
{
    string line;
    int value;

    while (true)
    {
        cout << "Please enter an integer: ";

        if (!getline(cin, line))
        {
            cerr << "Stream error detected!" << endl;

            // Error handling code here
        }
        else if (parse_int(line, value))
        {
            break;
        }
        else
        {
            cout << "Invalid input\n";
        }
    }

    cout << "You entered " << value << '\n';
}

"Flush" the stream (not recommended).
"flushing" streams can be tricky

Deceptikon,

Not trying to divert/hijack the thread here, but can you elaborate on this please? I've been flushing the stream after bad input for quite a while and it seems fairly straightforward. Perhaps I've been lucky. I don't disagree with you that using a stringstream and avoiding the headache altogether is generally preferable, but for example, would not using "ignore" solve the OP's problem ("goto" being a separate issue)?

cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );

Would this not work just fine? Again, I'm not disagreeing with your use of stringstreams, but I'm asking if you believe that sticking the line above after line 14 in the OP's code would also fix the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.