Jump to content
  • Advertisement
Sign in to follow this  
Sabonis

switch statement

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ++]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!