0

Like the code below :

int count = 0;
std::cin >> count_of_emploies;
for (int i = 0; i < 11 && (count <= 1 || count >= 100); i++) {
    std::cout << "enter again.";
    std::cin >> count;
    if (i > 9) return 0;
}

but when input was like "asdf" this code causes all the std::cin crush and the other part of the program almost work but without waiting for user's input.

kingsfoil
  • 3,795
  • 7
  • 32
  • 56

1 Answers1

1

when input was like "asdf" this code causes all the std::cin crush

That's because a "bad" input is received. To ignore this and continue, you will have to

a) clear the stream error flags std::cin.clear()

b) ignore the maximum possible characters in input buffer, until a newline using std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

P0W
  • 46,614
  • 9
  • 72
  • 119