I'm trying to create a text based game, and I have tried to create a menu with a switch statement, and a while loop.When I type 1 of the numbers, it works fine, but when I write another number that contains 1, 2, 3 or 4, it still runs, like 1344, and the more characters or numbers I write, the more lines of ""INVALID OPTION! Please select a valid menu..." I get. (see the code)
1. I suggest that menu() return the menu value rather than storing it in a global variable.
2. If you want 1234 to be invalid input then you should read a number, not a single character.
3. Do the error checking inside of menu(). In other words, menu() should always return a valid menu choice. If feels awkward, but this is a common case where a loop should exit from the middle. The basic pattern is:
1 2 3 4 5 6
give main prompt
while (true) {
get input
if input is valid then break
print "invalid input" error message
}