0

I am testing stuff, and seems like I found something that is not working as intended.

int main()
{
    int number;
    cout << "Please enter a number." << endl;
    cin >> number;
    while(!(cin >> number))
    {
        cout << "Please enter a number" << endl;
        cin.clear();
        cin.ignore(123, '\n');
    }
    cout << "number is " << number << endl;
    return 0;
}

The input validation works, for the most part. The only issue comes when the input is something like 35'5 or even 2I, which I would not want that input to be recognized as the number 35 or 2.

Does anyone know a solution for this?

Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696
  • 2
    "*I found something that is not working as intended*" - This is expected behavior, this is how `operator>>` is supposed to work. If you don't want this behavior, don't use `operator>>`. Read the input as a `std::string` and then validate the whole string, such as with `std::stoi()` or `std::stoul()` – Remy Lebeau Sep 09 '21 at 22:15

0 Answers0