Please help me with this code

Started by
8 comments, last by Tempest 22 years, 6 months ago
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.
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?
Use:
[ source ]
// Your code goes here
[ /source ]

but without any spaces within the [ and ].
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
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
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
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.
}
Joseph FernaldSoftware EngineerRed Storm Entertainment.------------------------The opinions expressed are that of the person postingand not that of Red Storm Entertainment.
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.
It replaces your normal cin statement.
Joseph FernaldSoftware EngineerRed Storm Entertainment.------------------------The opinions expressed are that of the person postingand not that of Red Storm Entertainment.
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..

This topic is closed to new replies.

Advertisement