Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Case statment help plz

When the user chooses option 3 the defaults from both case statments is called and it makes it break. What I want it to do is when they choose option 3 to move to the next case statement.

#include <iostream>
#include <ctime>
using namespace std;
void sleep(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
int main()

{

int input1, input2;

cout<<"One day Bbarry and Torkalis were walking through the woods when..\n";
sleep(1000);
cout<<"\t\t \t A shady figure descent jumps out! \n \n What now? \n 1.Try to fight him off \n 2. Run \n 3.Call for help\n";

cin>> input1;
switch (input1) {
case 1:
cout<<"Bbarry and Torkalis began fighting horonaly, right as they were about to defeat the lowly shadow man he pulled out a knife and stabbed them\n";
break;
case 2:
cout<<"Sensing danger Torkalis and Bbarry sprint away from the angry, poor shadow man. But to no avail, the black man pulls out a pistol and shoots them as they run then proceeds to belt out 'Yeeeee'\n";
break;
case 3:
cout<<"Torkalis shouts out for help, a nearby friendly stranger comes along and with a rifle and shoots the shadow man then introduces himself as Cesty\n";
sleep(1000);
switch (input2) {
case 1: cout<<"1\n";
case 2: cout<<"2\n"; break;break;
default: cout<<"That is not a valid option\n"; }


default:
cout<<"That is not a valid option\n";


}

return 0; }
[1656 byte] By [theillbehaviore] at [2007-11-11 10:29:42]
# 1 Re: Case statment help plz
you haven't asked the user for the input which will be assigned to input2
nspils at 2007-11-11 20:58:42 >
# 2 Re: Case statment help plz
break;break;
will not work you can use return statement or the bad goto
it career at 2007-11-11 20:59:42 >
# 3 Re: Case statment help plz
Hi,

if I were you I'd scrap the case - statements altogether. They're just a nuisance.
Use

if(input1 == 0)
{
...
}
else if(input1 == 1)
{
...
}
else if(input1 == 2)
{
if(input2 == 0)
{
...
}
else if(input2 == 1)
{
...
}
else
{
}
}
...
else
{
}

cascaded if statements instead. They will work if you decide to change the type of your input - variables as well. They are more flexible and I think more readable. The most imortant thing is: they don't have a break in the control-flow, so they are easier to verify and debug.

Cheers,

D
drkybelk at 2007-11-11 21:00:46 >
# 4 Re: Case statment help plz
Sorry for the mangled up code: typo... ;-)
drkybelk at 2007-11-11 21:01:41 >
# 5 Re: Case statment help plz
Thanks alot for the help guys. I think I got this now, kthnx
theillbehaviore at 2007-11-11 21:02:51 >