-1

There's a program where we are supposed to enter numbers via a while loop, and I want to quit the program by entering a "non-numerical" character. Could this be the code?

while (cin.fail())
{
  // block of code
}

Also, is there a special standard header for cin.fail()?

1 Answers1

1

There's no need to call cin.fail(). You can just do:

int n;
while (std::cin >> n)
{
    do_something_with_n (n);
}

Also, please don't spam tags. c++ plus the highest c++ version you have access to will suffice.

Paul Sanders
  • 15,937
  • 4
  • 18
  • 36