I've finished this game, my first one. It's just a simple one, but it works perfectly fine, at least for my first game.
The only problem I can't figure out how to fix is, if a user spams the known answers in one input (e.g "abbabba" then it skips the whole six other questions and gets to the score.
Is there a function for to stop this or should my code have been created differently?
Your problem arises from the fact that you doing a cin do a single char (player Input). The extra characters remain in the input buffer waiting for the next cin. You have a couple of choices:
- cin.ignore () to remove any extra characters from the input buffer. http://www.cplusplus.com/reference/istream/istream/ignore/
- Use a std::string for playerInput. Whatever the user typed will be placed in the string. If the user types extra characters, that won't match answera or answerb.
Some other comments:
You have a lot of repeated code in your program.
Lines 30-40 are the same for every question, except for the expected answer. Consider using a function and passing the expected answer as an argument to that function.
Lines 30-40: If the user enters something other than 'a' or 'b', you ignore the invalid input and proceed to the next question. You should prompt the user to enter 'a' or 'b' only before checking the answer.
Thank you for the reply and critique! While I don't fully understand what do to for the repeating code problem yet, I'm going to write some more programs using these to get used to them and learn more.
Hopefully I can get cin.ignore() working or std::string to not throw an error. But I'll keep going.
I know how annoying it can be to be bombarded with questions from someone who won't try, plus it's part of the learning experience.