Putting letters or decimal numbers in std::cin for int values causes infinite looping or code interruption
I'm writing a code but I'm having problems with user input options. Snippets like the following give me trouble.
int x;
do{
std::cin >> x;
// random code here
} while(x != y);
I have no issues with the code, and it works perfectly. But a typo in input time (as in code) causes the program to crash, like inputting a float or a char (where an integer should be). Entering a letter (and sometimes, maybe a decimal number) causes the code to stay in the while loop indefinitely, ignoring the input and often terminating the code after a few seconds.
I have no idea how to introduce a solution to this kind of problem, as there are a few dozen entries with this problem, and maybe this solution would take up a few dozen lines, which means I would need at least a hundred lines to solve just a user typo.
How does the std::cin function work during this user input issue, and how can I solve this problem in a simple way that doesn't turn my code into 10 functional lines and 20 lines of typo test?
Thanks in advance!