Hello c++,
This is my first post, so I figure I would start by saying Hello. I'm a student at Auburn University in a C++ class, and I've come here to get some advice as I am new to the C language. I'm almost done with my project, which is a text based video game. I currently have 4 classes to run this program. Player, Hall, Menu and Engine. The class Menu is a class to display the options available in the menu based game, and return the value entered by the user. Currently, if you enter a value that is not contained in the menu, my program goes haywire in a infinite loop that I can't figure out. I need to filter the input of the user to make sure that they enter a desired value so I wrote a function called bool isValidInput(int input). I can't figure out why my function isn't looping until the input I enter is valid. I think it has to do something with the fact that it gets the input from cin, and if that input it wrong it can't reset menuChoice using cin again because it already set menuChoice??? Help please!!! Here is a snippit of my code so you can get an idea of whats going on:
Make sure to add a cin.clear(); cin.sync(); in the do-while loop after the input. That way if they enter a character instead of an int, it can still recover from the error that occurs.
Thanks! Now it works fine for all integers that are inputted. But what about if a char or string is entered on accident? Say you enter 'c', then try and enter 1 to start the game. It won't change the value of menuChoice from c to 1. Is there a way to make menuChoice a general type so it can change from all variable types, but since i'm using isValidInput() it will only be set to a integer.....??
When you enter c, that is invalid input, and the stream goes into a state of error when trying to extract an integer. cin.clear() clears that state of error so you can use the stream again, then cin.sync() lines it back up again for the second go. Because you started your menuChoice at 0, and the extraction failed, it should still be 0, thus the isValidInput(menuChoice) should return false. This means that the loop should go around and try to take input again, thus you *should* be able to just type 1. If it doesn't work, then I'm not sure why, and I'll have to do a bit more testing.
I tried putting cin.clear(); cin.sync(); before and after the cin >> menuChoice; and neither worked. Heres a sample of my interactions:
MAIN MENU:
1) Start a New Game of Dunstan and Dragons!
2) View top 5 High Scores
3) Quit
Please choose an option: x
2
2
2
But if I were to enter a integer the entire time it wouldn't mess up at all. I think it is because once I initialize menuChoice as a char it freaks out and won't let me reinitialize it to an int??
(Bumping isn't really necessary here, topics not marked as resolved are distinguished on their own. Speaking of that, if this works for you can you tick the 'Resolved' button? Thanks :) )
Hmm, still didn't seem to fix the problem. Thanks for the reply though... I tried making menuChoice char instead of int to see if it would work, but that didn't either. I feel like menuChoice needs to be some generic type instead of int so that if a char is inputted menuChoice won't freak out by being initialized to a char.
Thanks for all the responses! Making menuChoice a char type changed everything.... Now I can enter string and char values, before a int is entered and MenuChoice is set to the right value. Thanks again for all the responses! Sorry I didn't give more of my code, it's 900 lines almost and I didn't really think it was necessary to submit all of it.