• Advertisement

Archived

This topic is now archived and is closed to further replies.

Please help me with this code

This topic is 5990 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

I''ve posted asking for a way to solve this, but can''t figure it out for some reason. This is a very small part of my code for an encounter. It works well, except that if the default case is run, it just prompts the user to "Press a key to continue" when I want to remain in the switch statement until a valid option is selected... int encounter() { cout << "An enemy approaches! What action will you take?\n\n"; bool exit = false; while (exit == false) { cout << "\t (1) Attack\n \t (2) Run\n\n"; cin >> option; switch (option) { case 1: cout << "\nYou strike at your opponent!\n\n"; break; case 2: cout << "\nYou turn and flee from your opponent!\n\n"; exit = true; break; default: cout << "\nYou must decide quickly!\n\n"; exit = true; break; }if (exit) break; } return 0; } See, when any key other than 1 or 2 is pressed, it says "You must decide quickly!" like it should, but then says "Press enter to continue" and exits the console. How can I make it list the options again without exiting? Please keep in mind this is a very stripped out portion of my code. If I can get this to work out, everything else should fit into it nicely. I have tried every combination of loops inside this one that I can think of. Thank you for your help.

Share this post


Link to post
Share on other sites
Advertisement
grr.. How do you folks get the code to be displayed inside the box, well formatted, rather than all along the side of the screen?

Share this post


Link to post
Share on other sites
hey,

i think what you should do is make your default exit false...

default:
cout << "\nYou must decide quickly!\n\n";
exit = false;
break;


this will keep it in the while loop until the user chooses 1 or 2.

also, i dont think that you need that if(exit) break; statement.

later,

DarkMonkey

Edited by - DarkMonkey on September 25, 2001 1:22:42 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
bool exit = true;
while (exit)
{
cout << "\t (1) Attack\n \t (2) Run\n\n";
cin >> option;

switch (option)
{
case 1:
cout << "\nYou strike at your opponent!\n\n";
exit=false;
break;

case 2:
cout << "\nYou turn and flee from your opponent!\n\n";
exit = false;
break;

default:
cout << "\nYou must decide quickly!\n\n";
break;
}

}// while(..)

I think that you want to make it stay at the while loop only if goes to default, right?
If that''s so, I think that this code should do the trick

Share this post


Link to post
Share on other sites
Thanks, I tried both of these things, but didn''t have any luck. If I take that out of the default area, then the program gets hung in a never ending loop for some reason.. I''ve tried all different types of alternatives.

How I have the loop set up now, when the attack option is selected, the menu comes up again (I''ll fill the actual battle function in here), as many times as the user presses 1, it will continually give them the option to either run or fight. If they select 2, the flee message is displayed and "enter to continue allows them to exit.

But, if any other key is pressed, like I said, the default statement is displayed and the program ends-this is what I don''t want. I want it to do like case 1 does and redisplay the menu. I''ve tried all the obvious combinations, and nothing seems to work. Why is this that difficult? Would someone actually try to similate this and see if they can help me figure it out?

Thanks for all your help. This is pretty frustrating

Share this post


Link to post
Share on other sites
What''s happening here is that when you enter your data you press the "Enter key" which leaves a ''\n'' in your input buffer. What you need to do is check the input stream and get rid of the extra ''\n'' that''s in there. This also needs to be done to safe guard your program from a stupid moron like myself hitting a instead of 1 . Here is an example of how to do it:

while(! (cin >> option) )
{
cin.clear();
while( cin.get() != ''\n'' )
{
continue;
}
// Possibly prompt user again for correct input.
}

Share this post


Link to post
Share on other sites
Hey, thanks evaclear! That sounds like a pretty cool idea.
I''m sorry to be such a newbie, but where exactly in my code are you suggesting I stick something like that?

Thank you all for being patient with me.

Share this post


Link to post
Share on other sites
Why don''t you try something like this..

int encounter()
{
cout << "An enemy approaches! What action will you take?\n\n";
bool exit = false;
while (exit == false)
{
cout << "\t (1) Attack\n \t (2) Run\n\n";
cin >> option;

switch (option)
{
case 1:
cout << "\nYou strike at your opponent!\n\n";
exit = true;
break;
case 2:
cout << "\nYou turn and flee from your opponent!\n\n";
exit = true;
break;
default:
cout << "\nYou must decide quickly!\n\n";
break;
}//end switch
}//end while

return 0;
}

This should exit if they press 1 or 2.. but ask again if they press something other than those.

Hope this helps..

Share this post


Link to post
Share on other sites

  • Advertisement