Hey, I am new to the programming world and I need some help with a program that I am writing. The program is a game where the user inputs a number between 1 and 10 and the program generates a random number within that same criteria. The problem arises when the user inputs a character such as a letter or a symbol. It will loop endlessly until the command line is closed. I apologize in advance if my code is very amateurish. I am just now learning how to code at a community college and we've only learned flowcharting and pseudocode so far. I'll begin learning actual code starting this week. Also this code is C++. Thanks for the help and I am open to any tips.
when you read a character into a variable expecting an integral value, an error state is set on the stream immediately, the looping behavior you're getting is because the read will not be successful if an error state have been set on a stream, so your program will always be trying to read from the stream with no success, to solve that you'll need to clear the error state by calling the stream clear function cin.clear()
Advice : gotos will never be as friendly as they might seem, learn to avoid them as soon as
possible, the language offers a variety of loops that will achieve what you intend.
Are you asking how to validate user input before setting the character. If so you can do something as easy as setting the character then testing its value using a condition statement and if after testing the character it is set to something other than the requested values you could reset the character.
I have fixed the problem. I did some asking around and a little more research have come upon the fix. Also got rid of the spaghetti code that is caused by the goto functions. Not sure if generated more but the program has a few more fancy features.