I'm not sure exactly what causes the errors, but here are some clues that might fix them...
1) the best input validation for standard cin would go has follow
1 2 3 4 5 6 7 8 9 10 11 12
|
cout << "Please pick up your toothpick(s), choose between 1 and 3.\n";
cin >> h_pick;
while(cin.fail() || cin.get() != 10 || h_pick < 1 || h_pick > 3 || h_pick > toothpicks)
{
cin.sync();
cin.clear();
cout << "Please pick up your toothpick(s), choose between 1 and 3.\n";
cin >> h_pick;
}
|
cin.fail()
will verify that you didn't crash it, for example, if you input letters instead of a number,
cin.get() != 10
will verify that the last character sent to cin is a newline character (instead of a space with more numbers after, for example), and the last three conditions are the ones specific to the input expected by your program. It's in a while loop, so if any of the conditions return true, your input sequence simply restarts, that is until a valid value for h_pick is entered.
cin.sync()
and
cin.clear()
will just reset cin in case it failed or if you entered gibberrish. But the validation sequence must be RIGHT AFTER each call to cin, because if you enter an invalid value, your toothpicks total will be screwed and it will screw the AI's choice, so line 67 to 72 are useless.
2) In line 35 and 62, I think you just need to verify if
toothpicks == 1
, because at this point it doesn't matter what the user's pick is, the game is over.
3) I know what else you should fix but I'll let you go with that, reply if you have more questions :-)
Nice game by the way! ^_^
AeonFlux