Return to start of program

Dec 11, 2008 at 9:35pm
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);
           }     
        else if (ans == c || C)
           {
             
           }
        else (ans !=  q || c );
          {
           cout << "Invalid choice";     
          }                     
     break;
Dec 11, 2008 at 9:45pm
You could use this construction:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)
Last edited on Dec 11, 2008 at 9:45pm
Dec 11, 2008 at 9:45pm
your or statements are wrong, you must replace
else (ans != q || c );

for

else (ans != q ||ans != c );

the same with all of them with the same structure
Dec 11, 2008 at 9:55pm
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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??
           }     
        else if (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;
Last edited on Dec 11, 2008 at 9:55pm
Dec 11, 2008 at 9:58pm
char q, Q, c, C

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.
Last edited on Dec 11, 2008 at 10:01pm
Dec 11, 2008 at 10:02pm
o yer i see what you mean my mistake..changed that now
Dec 11, 2008 at 10:22pm
the exit program part is still throwing me errors. Whats the best wayt o amke it just exit completely?
Dec 11, 2008 at 10:48pm
The best way to exit would be: return 0

Try something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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;
           }     
        else if (ans == 'c' || ans == 'C')
           {
                 bRestart = false;   
           }
        else if (ans != 'q' && ans != 'Q' && ans != 'c' && and != 'C');
          {
                 cout << "ERROR" << endl;
                 cout << "Are you sure?";
          }                     
     break;

} while( bRestart == true );

Dec 12, 2008 at 3:29pm
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
           }
else if (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
}     
Topic archived. No new replies allowed.