Error proofing my switch statement.

This is a discussion on Error proofing my switch statement. within the C++ Programming forums, part of the General Programming Boards category; Okay so I have a switch inside a loop and everything works fine and dandy until I enter a character ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    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?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Shamino View Post
    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 .
    Where's your default case? I only see 0, 1 & 2.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Let me guess: IO::instance().get_input() reads an int, but if the user enters non-integral input, you get a failed read.

    What you can do is alter the implementation of get_input() to check if the read succeeds. If it does not, it feedbacks to the user and requires the user to enter input again. At the same time it clears the error state of the stream (e.g., cin.clear() ) and discards the input (e.g., with cin.ignore() for a suitable number of characters). This can be done in a loop until the user enters integral input.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Quote Originally Posted by cpjust View Post
    Where's your default case? I only see 0, 1 & 2.
    Sorry, yes the main loop has no default case because the switch on the controller variable is all internal and does not require user input. However menu selections do require user input so they need a default, the default switches are inside the functions run in the controller switch.

    Laserlight I believe you've pretty much confined the problem to my get_input function, it does request integral data but if it does not get it the read does indeed fail.

    How do I determine whether or not my read input is failed? I've never actually checked input like this.

    Code:
    int IO::get_input()
    {
    	std::cin >> input;
    	return input;
    }
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Code:
    int IO::get_input()
    {
        // assuming input is a member variable...
        while (!(std::cin >> input))
        {
            // clear error flags
            // ignore what remains in the input buffer
            // optionally, ask user to enter input again
        }
        return input;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Thanks very much, that cleared up the problem just fine.

    I have another question though, is there a way to not use windows functions but still get keyboard events? Like the up arrow key and down arroy key in a console window?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Controller never gets reset if it gets set to an invalid value. The first time a bad value goes in, you go into an infinite loop. You need a default case which breaks the outer loop.

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Controller should never get a bad value unless I program something wrong, the variable controller is controlled internally, whereas the functions I perform to set controller have switches in them that require user input, which can be error prone. I guess I should have showed you the functions themselves rather than the main event loop. Basically controller getting a bad value would be only due to programmer fault and not the user themselves.

    The functions being used to control the controller variable are the ones getting bad values, but that was due to the get_input function not checking for bad input.

    I have a question, whenever I get some bad input, and i request input again, it makes me input it twice, how do I prevent this from happening?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by Shamino View Post
    Controller should never get a bad value unless I program something wrong, the variable controller is controlled internally, whereas the functions I perform to set controller have switches in them that require user input, which can be error prone. I guess I should have showed you the functions themselves rather than the main event loop. Basically controller getting a bad value would be only due to programmer fault and not the user themselves.
    But code goes wrong all the time Why not just make it bullet proof?

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by Shamino View Post
    I have a question, whenever I get some bad input, and i request input again, it makes me input it twice, how do I prevent this from happening?
    This sounds like that standard "not flushing stdin" problem. It's in the FAQ.

  11. #11
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    oh, I was flushing it, I was just adding another cin call that was unneeded, bahah.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. switch statement issues...(undeclared identifiers)
    By mero24 in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2005, 07:05 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21