Error proofing my switch statement.
Okay so I have a switch inside a loop and everything works fine and dandy until I enter a character into the switch. I realize that the switch is made on an integer, the problem is I figured the default case would take care of anything that wasn't one of the cases, but instead if a character is entered it goes into an infinate loop :(.
Here is my code:
Code:
void Card_Game::play_game()
{
//shuffle the deck first
deck.shuffle_deck();
//draw cards for players
Linked_List<std::string> output_list;
int gameloop = 1;
int result = 0;
int controller = 0;
while (gameloop != 0)
{
IO::instance().clear_screen();
switch(controller)
{
case 0:
// main menu
std::cout << "Welcome to the Casino!\n";
std::cout << "What would you like to do?\n";
std::cout << "1. Play Blackjack\n";
std::cout << "2. Quit\n";
result = IO::instance().get_input();
controller = main_menu(result);
if (controller == 3)
{
gameloop = 0;
}
break;
case 1:
// blackjack function
std::cout << "What would you like to do?\n";
std::cout << "1. Draw a card\n";
std::cout << "2. Stay\n";
std::cout << "3. Fold\n";
result = IO::instance().get_input();
controller = blackjack(result);
break;
case 2:
// deal again?
std::cout << "Would you like to deal again?\n";
std::cout << "1. Yes\n";
std::cout << "2. No\n";
result = IO::instance().get_input();
controller = deal_again(result);
break;
}
}
}
What do I need to add to make it so if the user enters a character it doesn't break my switch? Or should I use a new form of input alltogether? Perhaps a character or string?