Create a MENU using Switch,If-else if, goto

Pages: 12
@againtry. check it out
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 #include <iostream>
  #include <cstdlib> 
  #include <ctime>

  using namespace std;
  
  int main()
  {
      srand(time(0));
  
        int a,b,c;
     
       bool A=0, B=0, C=0, R=0;  //  0 or 1  => false or true
    
    M:
        
    system("cls") ;
      
     cout<<"\n\n\t\t MENIU"<<endl;
     cout<<"\t  1. Enter a"<<endl;
     cout<<"\t  2. Enter b"<<endl;
     cout<<"\t  3. Enter c"<<endl;
     cout<<"\t  4. a+b-c"<<endl;
     cout<<"\t  5. show Rez"<<endl;
     cout<<"\t  0. EXIT"<<endl;
     int Key;
     cin>>Key;
     
     switch (Key)
    {
        case 1: cout<<"Enter a=";
                cin>>a;    
                A=1;                
                break;
            
        case 2: cout<<"Enter b=";
                cin>>b;    
                B=1;
                break;
                
        case 3: cout<<"Enter c=";
                cin>>c; 
                C=1;
                break;
                
        case 4: 
                  if( A == 0 )
 {   cout<<" Error:: Enter a "<<endl;    }
                  if( B == 0 ) 
{   cout<<" Error:: Enter b "<<endl;    }
                  if( C == 0 ) 
{   cout<<" Error:: Enter c "<<endl;    }
                  
                  if( A==1 && B==1 && C==1 )
{  cout<<" a+b-c  "<<endl; R=1; } 
                  
                      break;
        
        case 5:   if( A == 0 )
 {   cout<<" Error:: Enter a "<<endl;    }
                  if( B == 0 )
 {   cout<<" Error:: Enter b "<<endl;    }
                  if( C == 0 )
 {   cout<<" Error:: Enter c "<<endl;    }
                  if( R == 0 )
 {   cout<<" Error:: a+b-c "<<endl;    }
        
                  if(R==1){
                      // A=0;
                      // B=0;
                      // C=0;
                      R=0;
                      cout<<" a="<<a<<" b="<<b<<" c="<<c<<" a+b-c  R="<<a+b-c<<endl;
                  } 
        
                break;
        
        case 0: goto EXIT;
        
    }
    
     
     system("pause");
     
     goto M;
     
     EXIT:
     
        
      return 0;
  }
Last edited on
Will do ... back shortly
Here's a couple of points to consider:

- line 3 and 9: where are these being used. If not then remove.

- line 11: initialize all variables - int a = 0, etc

- line 13: bool is good but instead of 0's and 1's try 'false', 'true' instead

- a, b, c as names are OK but more meaningful names are better for the rest - eg 'result', bool bValueFilled instead of B etc, allValuesFilled for R - or whatever you want to call them so the code is readable and self documenting.

- line 55: it might be a good move to print the answer out as well :)

- how come your switch hasn't got a default: case?

- as a matter of style and readability, putting multiple instructions on a line is very bad practice. It makes the code much more readable and checkable to keep it simple. You're not programming in the 1950's with a white coat and nearly no memory.

Otherwise looks OK on a brief test and you're on the right track.

All the best.


Topic archived. No new replies allowed.
Pages: 12