7.16 — std::cin and handling invalid input – Learn C++

PHOTO EMBED

Wed Mar 16 2022 19:08:15 GMT+0000 (Coordinated Universal Time)

Saved by @pham24n #c++

if (std::cin.fail()) // has a previous extraction failed or overflowed?
{
    // yep, so let's handle the failure
    std::cin.clear(); // put us back in 'normal' operation mode
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // and remove the bad input
}
content_copyCOPY

https://www.learncpp.com/cpp-tutorial/stdcin-and-handling-invalid-input/