Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

check if cin got the wrong type

while (std::cout << "Enter a number" && !(std::cin >> num)) {
    std::cin.clear(); //clear bad input flag
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '
'); //discard input
    std::cout << "Invalid input; please re-enter.
";
}
 
PREVIOUS NEXT
Tagged: #check #cin #wrong #type
ADD COMMENT
Topic
Name
2+5 =