switch statement

Started by
12 comments, last by KittyRa 18 years, 7 months ago
Hey everyone! Just a quick question: (using c++ and visual studio 6.0, making text game) I have a menu that uses a switch statement to determine what the user wants to do in that menu. For example, the first line has a list of options (1. New Game, 2.Load game etc) It then waits for user input in the form of an integer. If the user puts in a character, it goes into infinite loop and I am trying to stop that. The default: portion of my switch statement is not protecting against this... What can I do?
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Advertisement
This problem actually has Nothing to do with Switch. see, when cin gets a bad input it goes into an error state which you can do a test against. This page seems like a very good explanation of the situation.

http://cplus.about.com/od/cprogrammingtip1/l/aa030702b.htm

Hope this helps.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

What are you using to read user input?
im using the cin >> function to get input
thanks!
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
nobodynews is correct. Add in some of the code from the link he posted and you should be ok. Anytime you ask for user input, you should check the return value and handle exceptions.
I was also thinking of casting the input into an integer when its in the switch statement

example

int choice;
cout << "Please enter your choice as an integer: \n";
cin >> choice;

switch(int(choice))
{
case 1:
//blabla
break;
......
default:
cout << "Invalid Choice";
}

would this work, or would cin still have the error flags going?


[edit - THANKS GUYS FOR THE QUICK HELP!! RATINGS ++]
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
I'm not sure. I usually just use the !cin method since that is what I was taught at school.

Try it and see if it breaks.
You can't cast a character directly into an integer the way you are doing it and expect it to work right. When you cast a char into an int, the int value returned is the integer ASCII value of that char. For example, casting the character 'a' into an int would give the integer value 65. To properly convert from a string (char) representation of a number into an actual number, you need to use the atoi(...) function (or atof if dealing with floats/doubles). See here for an explanation of the atoi function: http://www.cplusplus.com/ref/cstdlib/atoi.html.

Hope that helps.
Slightly off-topic:
Try boost::lexical_cast.
Download boost here.
Using it, you can simply
std::string str = "123";int i = boost::lexical_cast<int>( str );

int i = 123;std::string str = boost::lexical_cast<std::string>( i );

If the cast fails, it throws a bad_lexical_cast exception.
Don't know if this works for MSVC6 (deprecated), so you might want to switch to Visual C++ 2005 Beta or Visual Studio 2003 Toolkit (free optimizing command line compiler, the one from Visual Studio .NET 2003), which have vastly better C++ standards conformance and work well with boost.
thanks for the reply!
The reason why I'm not concerned with the atoi( function is because I want to test only for the case numbers. Anything else should be dealt with as an error.
My main objective was to try and cast the input into an int regardless and see if the cin error flags can be avoided.
I'm trying this right now I will reply shortly
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...

This topic is closed to new replies.

Advertisement