CHAR.SWITCH

Hey lads, need your help in here,
Using the Switch operator to create a menu like below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{    
     while(1)
     {    
          system("cls");
        switch(meniu())
        {
 case     49   : system("cls"); insert(); break; 
 case     50   : system("cls"); show(); system("pause"); break; 
 case     51   : system("cls"); show(Media(0),0); system("pause"); break;
 case     52   : system("cls"); show(Media(1),1); system("pause"); break; 
 case     53   : system("cls"); find(); system("pause"); break; 
 case     54   : system("cls"); findgroup(); system("pause"); break; 
 case     55   : system("cls"); findmedia(); system("pause"); break; 
 case     56   : system("cls"); sort(); system("pause"); break; 
 case     57   : system("cls"); sortmedia(); system("pause"); break;
 case     ?   : system("cls"); sortgen(); system("pause"); break;
 case     27   : exit(0);
    }
  }
 return 0;
}


what would be the case nr. after the 57 one? (which should be 10)
how do I represent it?
I though of giving the choice for user to press a letter instead of a number, but i am convinced there is a way to reproduce number 10.

Many thanks1
Last edited on
What does meniu() do and what does it return?

If the numbers are ASCII, then 49 is '1', 57 is '9', 27 is ESC. On this basis 10 would be 58 (':') which is probably not what you want.

Rather than using char numbers, what about letters as per your post? This would give 26 options.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{    
     while(1)
     {    
          system("cls");
        switch(meniu())
        {
 case     'a'   : system("cls"); insert(); break; 
 case     'b'   : system("cls"); show(); system("pause"); break; 
 case     'c'   : system("cls"); show(Media(0),0); system("pause"); break;
 case     'd'   : system("cls"); show(Media(1),1); system("pause"); break; 
 case     'e'   : system("cls"); find(); system("pause"); break; 
 case     'f'   : system("cls"); findgroup(); system("pause"); break; 
 case     'g'   : system("cls"); findmedia(); system("pause"); break; 
 case     'h'   : system("cls"); sort(); system("pause"); break; 
 case     'i'   : system("cls"); sortmedia(); system("pause"); break;
 case     'j'  : system("cls"); sortgen(); system("pause"); break;
 case     27   : exit(0);
    }
  }
 return 0;
}


If you really want the user to enter 10 as chars - and still return a char, then meniu() will need to be able to accept 10 as a string input and convert to say : which can then be used as a case statement.
Last edited on
what would be the case nr. after the 57 one? (which should be 10)
how do I represent it?
1
2
3
 case     57   : system("cls"); sortmedia(); system("pause"); break;
 case     10   : system("cls"); sortgen(); system("pause"); break;
 case     27   : exit(0);
Hello MaxGreen,

Something to consider:
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
#define CLS system("cls")
#define PAUSE system("pause")

int main()
{
    while (1)
    {
        CLS;

        switch (meniu())
        {
            case     49: CLS; insert(); break;

            case     50: CLS; show(); PAUSE; break;

            case     51: system("cls"); show(Media(0), 0); system("pause"); break;
            case     52: system("cls"); show(Media(1), 1); system("pause"); break;
            case     53: system("cls"); find(); system("pause"); break;
            case     54: system("cls"); findgroup(); system("pause"); break;
            case     55: system("cls"); findmedia(); system("pause"); break;
            case     56: system("cls"); sort(); system("pause"); break;
            case     57: system("cls"); sortmedia(); system("pause"); break;

            case     10 : system("cls"); sortgen(); system("pause"); break;

            case     27: exit(0);
        }
    }

    return 0;
}

Watch your indenting.

The space between "case" and the number is unnecessary, but if you have a reason it does not matter. The compiler will just ignore the spaces.

coder777's example of using 10 can work, but it would depend on what the "meniu" function returns. Without seeing the "meniu" function it is hard to advise on what to do not knowing what it returns.

The "#defines", maybe not the best choice is a way to make the code a little cleaner. Use if you like.

As per seeplus's example I have found that when menu choices exceed the number 9 it is better to use letters. And if you consider upper case and lower case as separate you can have 52 choices.

Andy
If meniu() returns type char, then 10 is '\n' which is probably not what the OP wants - as sort() will be executed when a newline is entered....
If meniu() returns type char, then 10 is '\n' which is probably not what the OP wants - as sort() will be executed when a newline is entered....
I would think that it returns only expected values. Then 10 is an distinct value like the other too.
thank you guys,
i used a char name , ex. 'a' after the 9-th menu option.

sortgen() function was sorting ascending the inserted students due to their sex(male, female).

menu() was returning char, double and bool values as well.
Last edited on
Topic archived. No new replies allowed.