Hi all
im having abit of trouble getting one of my if statements to return back to the start of the program, i have tried several different ways and haven't had any luck. Any help would be appreciated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
case'q':
case'Q':
cout << "Are you sure you want to quit? Type Q to quit or C to continue.\n";
char q, Q, c, C;
cin >> ans;
if (ans == q || Q)
{
exit(0);
}
elseif (ans == c || C)
{
bool goOn;
do
{
//... your main-loop
goOn=false //make sure the program wont keep on repeating
//the switch:
case'Q':
if() //if program should return
{ goOn=true}
break;
//end switch
if (!goOn) //I guess the rest shoulnt be executed
{}//rest of your main
//end do-while loop:
} while (goOn)
i tried what scipio mentioned but had no success...could it be that im using a switch statement? or does that not make any difference?..this is what i have got:
Room*Room:: moveDirection(char userChoice)
{
//use a switch statement to go to a certain case depending on the user input
//gets the users input userChoice
switch(userChoice)
{
case'n':
case'N':
//returns the north location
return m_North;
break;
case'e':
case'E':
//returns the east location
return m_East;
break;
case's':
case'S':
//returns the south location
return m_South;
break;
case'w':
case'W':
//returns the west location
return m_West;
break;
case'q':
case'Q':
//the quit feature
//gives the user the option to continue or quit
cout << "Are you sure you want to quit? Type Q to quit or C to continue.\n";
char q, Q, c, C;
//gets the users input ans
cin >> ans;
if (ans == q || ans == Q)
{
//<-exit program??
}
elseif (ans == c || ans == C)
{
//<- continues at same position
}
else (ans != q ||ans != c );
{
//<- displays an error and then the cout << are you sure.... part again
}
break;
Here you declare the names of variables of type char, wich has nothing to do with their actual value. If ans is of type char to, you could use this:
if (ans == 'q' || ans == 'Q')
Btw. The fact that you're using a switch-statement does not in any way effect how the commands in it are executed, only when. What could be a problem however, is the scoop of variables: you need to declare goOn at the beginning of main(), and not at the beginning of the switch statement, so it you can access it from the whole main body.
do {
bool bRestart = true;
//( . . . )
Room*Room:: moveDirection(char userChoice)
{
//use a switch statement to go to a certain case depending on the user input
//gets the users input userChoice
switch(userChoice)
{
case'n':
case'N':
//returns the north location
return m_North;
break;
case'e':
case'E':
//returns the east location
return m_East;
break;
case's':
case'S':
//returns the south location
return m_South;
break;
case'w':
case'W':
//returns the west location
return m_West;
break;
case'q':
case'Q':
//the quit feature
//gives the user the option to continue or quit
cout << "Are you sure you want to quit? Type Q to quit or C to continue.\n";
char q, Q, c, C;
//gets the users input ans
cin >> ans;
if (ans == 'q' || ans == 'Q')
{
return 0;
}
elseif (ans == 'c' || ans == 'C')
{
bRestart = false;
}
elseif (ans != 'q' && ans != 'Q' && ans != 'c' && and != 'C');
{
cout << "ERROR" << endl;
cout << "Are you sure?";
}
break;
} while( bRestart == true );
If you use the construction of CheesyBeefy, you don't have to use the variable bRestart. Instead, you could use an *infinitive* loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
while (true)
{
//...
if (ans == 'q' || ans == 'Q')
{
return 0; //here it quits: it doesnt exucete anyting else
}
elseif (ans != 'q' && ans != 'Q' && ans != 'c' && and != 'C');
{
cout << "ERROR" << endl;
cout << "Are you sure?";
}
break;
//If the user entered he want to quit, the program won't reach this point,
//so there is no need for an extra variable
}