Switch case menu help

Status
Not open for further replies.

cnandha19

Member level 3
Joined
Aug 28, 2015
Messages
59
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
604
If i pressed 2 it goes to load game(); and then if i pressed Enter key to go next menu that is Playgame ();

and from playgame(); i press escape key to go back to loadgame(); please help how todo this with switch case


Code C - [expand]
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
#include <stdio.h>
        
        void playgame()
        {
            printf( "Play game called" );
        }
        void loadgame()
        {
            printf( "Load game called" );
        }
        void playmultiplayer()
        {
            printf( "Play multiplayer game called" );
        }
            
        int main()
        {
            int input;
        
            printf( "1. Play game\n" );
            printf( "2. Load game\n" );
            printf( "3. Play multiplayer\n" );
            printf( "4. Exit\n" );
            printf( "Selection: " );
            scanf( "%d", &input );
            switch ( input ) {
                case 1:            /* Note the colon, not a semicolon */
                    playgame();
                    break;
                case 2:          
                    loadgame();   
    
        
                    Enterkey();
         
    
    
    
           
        
                   playgame();
                    
                    break;
                case 3:         
                    playmultiplayer();
                    break;
                case 4:        
                    printf( "Thanks for playing!\n" );
                    break;
                default:            
                    printf( "Bad input, quitting!\n" );
                    break;
            }
            getchar();
            enter code here
        
        }

 

Instead of using the input variable to decide which action to do, you could deal with another variable representing the current state. With that approach, you should call playgame() either from the case option '1' or from the new state just created.
 
How sir can you please reply with example
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…