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

Pages: 12
Hey lads,
As you know I am only at the beginning so don't judge me too hard.
I have to write a code using "switch", "if-else if" and "goto" which will be a Menu that will conclude 5 different options as follow below:
1. Choose a
2. Choose b
3. Choose c
4. Calculate a+b-c
5. Check the result.

The trick is that the user have to type a, then b, then c, in order to get to calculate a+b-c and only afterwards to check the result.
In other words each of these steps are being a mandatory from 1 to 5.
When run the app if you choose straight away option 4 it should say that: "type, a,b and c to calculate" or if you give values for just a and b, it should ask you to provide a value for c as well and so on. I hope you got my idea.
Check the code below and hope it will easy the explanation(it doesn't work as i want to, hope you help me folks:


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
    #include <iostream>
  #include <ctime>

  using namespace std;
  

  int main()
  {
      srand(time(0));
	  
	  int a, b, c, R;
        int MENU;
                 M:
        system("cls"); 
        
        cout<<" \n\t MENU"<<endl;
        cout<<" \t 1. Enter a "<<endl;
        cout<<" \t 2. Enter b "<<endl;
        cout<<" \t 3. Enter c "<<endl;
        cout<<" \t 4. Calculate a+b-c "<<endl;
        cout<<" \t 5. Check the result "<<endl;
        cout<<" \t Choose from 1 to 5 ::";
        cin>>MENU;
        
        cout<<endl;
        
        switch(MENU)
        {
            case 1: 
                   cout<<" Choose a value for a "<<endl;
				   cin>>a; 
				   cout<<" a= "<<a<<endl;
                
                    break;
            case 2: cout<<" Choose a value for b "<<endl;
                    cin>>b; cout<<" b= "<<b<<endl;
                
                    break;
            
            case 3: cout<<" Choose a value for c "<<endl; 
			        cin>>c; cout<<" c= "<<c<<endl;
                
                    break;
            case 4: if (R=a+b-c) cout<<" Calculate the result for a+b-c, which is equal to "<<a+b-c<<endl;
                    else cout<<"Choose a,b,c"<<endl;
                    break;
                    
                    
            case 5: if(R)cout<<"Result is "<<R<<endl;
			        else cout<<"Choose option 4 to calculate the equation "<<endl;
                    break;
            default: cout<<" Impossible option, choose wisely "<<endl;
        } 
        system("pause");
        
        goto M;
         
                     
        
        
      return 0;
  }
Last edited on
I'd build on something like this.

BTW congesting multiple instructions on single lines and not using whitespace and brackets isn't programming. All it does is make the code difficult to read and debug which is very unprofessional.

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
#include <iostream>

using namespace std;

int main()
{
    int a{0}, b{0}, c{0}, R{0};
    int MENU;

    // EDIT small change
    bool a_selected = false;
    bool b_selected = false;
    bool c_selected = false; // <-- etc as required
    
menu:
    cout<<" MENU\n";
    cout<<" 1. Enter a\n";
    cout<<" 2. Enter b\n";
    cout<<" 3. Enter c\n";
    cout<<" 4. Calculate a+b-c\n";
    cout<<" 5. Check the result\n";
    cout<<" Choose from 1 to 5 :: ";
    cin>>MENU;

    switch(MENU)
    {
        case 1:
            if(a_selected == false)
            {
                cout<<" Choose a value for a "<< endl;
                cin>>a;
                cout<<" a= "<<a<<endl;
                a_selected = true;
            }
            else
                goto menu;
            break;
            
        case 2:
            if(a_selected == true)
            {
                cout<<" Choose a value for b "<<endl;
                cin>>b; cout<<" b= "<<b<<endl; // <-- whatever that means :(
                b_selected = true;
            }
            else
            {
                cout << "Enter a first\n";
                goto menu;
            }
            break;
            
        case 3:
            cout<<" Choose a value for c "<<endl;
            cin>>c; cout<<" c= "<<c<<endl; // should be 2 lines
            
            break;
            
        case 4:
            if (R=a+b-c)
            {
                cout
                << " Calculate the result for a+b-c, which is equal to "
                <<a+b-c<< '\n';
            }
            else
                cout<<"Choose a,b,c\n";
            break;

        case 5:
            if(R)
                cout<<"Result is "<<R<<endl;
            else
                cout<<"Choose option 4 to calculate the equation\n";
            break;
            
        default:
            cout<<" Impossible option, choose wisely\n";
            goto menu;
    }
    return 0;
}
Last edited on
@againtry thank you for your tips and help, I will keep in mind and apply your advice
:)
Hello MaxGreen,

Here is a little different approach to your program.
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
92
93
94
95
96
97
#include <iostream>
//#include <cstdlib>  // <--- "srand()" and "rand()".
//#include <ctime>

using namespace std;

int main()
{
    //srand(time(0));
    //srand(static_cast<unsigned int>(time(nullptr)));

    int a{}, b{}, c{}, result{};
    int menuChoice{};

M:

    system("cls");

    cout <<
        "\n\t      Menu Choice\n"
        "\t-----------------------\n"
        "\t 1. Enter a\n"
        "\t 2. Enter b\n"
        "\t 3. Enter c \n"
        "\t 4. Calculate a+b-c\n"
        "\t 5. Check the result\n"
        "\t 6. Exit\n"
        "\t Choose from 1 to 6 :: ";
    cin >> menuChoice;

    cout << endl;  // <--- Could be written as: "cout << '\n';".

    switch (menuChoice)
    {
        case 1:
            cout << " Choose a value for a: ";
            cin >> a;

            cout << " a = " << a << endl;

            break;

        case 2:
            cout << " Choose a value for b " << endl;
            cin >> b;
            
            cout << " b= " << b << endl;

            break;

        case 3:
            cout << " Choose a value for c " << endl;
            cin >> c;

            cout << " c= " << c << endl;

            break;

        case 4:
            if (result = a + b - c)
                cout << " Calculate the result for a+b-c, which is equal to " << a + b - c << endl;
            else
                cout << "Choose a,b,c" << endl;

            break;

        case 5:
            if(result)
                cout << "Result is " << result << endl;
            else
                cout << "Choose option 4 to calculate the equation " << endl;

            break;

        case 6:
            goto exit;

        default:
            cout << " Impossible option, choose wisely " << endl;
    }

    //system("pause");

	// A fair C++ replacement for "system("pause")". Or a way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.

    goto M;

exit:

    return 0;
}

I have not idea what IDE/compiler you are using.

For your include files. Do not count on "iostream" including the header file "cstdlib" because it may not. VS does include the header file "xlocnum" which then includes:
1
2
3
4
5
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <streambuf> 

but the MIN GW compiles does not include these header files. So it is better to include the header files that you need and let the header guards keep something from being included more than once.

The 2 header files that are commented out are because you do not need or use them.

In "main" line 10 is a better way to write "srand". Looking at "srand" the parameter requires an "unsigned int" for its variable type, but "time" does not return an "unsigned int" hence the type case. From C++11 on "nullptr" is the better choice over "0" or "NULL". And if you are going to use "rand" this video is worth watching: https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful At least it will give you a better idea of how you can use "rand".

Since you never use "rand()" in the program these lines are commented out.

Next it always a good idea to initialize your variables when they are defined. From C++11 on you have the uniform initializer of the {}s. Empty they will set the variable to zero based on the variables' type. You can also put anything needed between the {}s to initialize the variable to a given value.

Next would be to give the variables a better name other than a single letter. AS you progress you will find this to be a much greater benefit when coding because it is easier to understand what the variables are.

Your 2 lines using "system" should be avoided as this can leave your program open to hackers who could gain access to your computer. If you are interested I have a function that will clear the screen and can show you how to use it. For the pause I use this:
1
2
3
4
5
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();


For the menu I have found this to be helpful. The way it is laid out usually gives a good representation of what it will look like on the screen. Since the "cout" can be chained together you only need 1. Making use of the "\n"s eliminates all the "endl"s which create overhead and cause a program to run slower than needed.

The other part that you can use to your advantage is for example lines 19 - 28 is 1 "cout" statement, but the "cin" on line 29 will flush the output buffer and print the entire "cout" to the screen before any input is allowed. So if the "\n"s do not flush the output buffer the "cin" will. Save the "endl"s for when you really need them.

I added the 6th choice because there was no way to end the program.

For the most part the "switch" is OK. I did add the "case 6" to end the program.

I have not worked on it yet, but againtry's idea of the bool variables is a possible solution as to whether you have a value for your variables or need to enter values.

In "case 4" the if statement is good, but you are going about it the wrong way. Consider "1 + 2 - 3" the result would be zero which would cause the if statement to be false and execute the else part even if it should not. Using the bool variables you could do something like this:
1
2
3
4
5
6
7
8
9
10
case 4:
    if (a_selected && b_selected, && c_selected)
    {
        result = a + b - c;
        cout << " Calculate the result for a+b-c, which is equal to " << result << endl;
    }
    else
        cout << "Choose a,b,c" << endl;

    break;

I have not tested this yet, but it should work.

The same idea would work for "case 5", but it would be best if you have a 4th bool variable to show that you have actually done step 4. Again because "result" could be zero.

For the "goto" part and forgive mu bluntness, but the person teaching this should be shot, drawn and quartered and have a wooden stake driven into their heart.Although the "goto" statement had a limited use it is bad form for general use, as done here. A do/while or while loop can replace the "goto".

The "goto" is an unconditional jump to its label and does not care about where it came from or if it would cause a problem.

I realize you may not have learned about loops yet, but understand the "goto" is not the best way to program.

Andy
Hello Andy, thank you so much for all the info provided and so much explanation. Hope to get to your level some time :))
Btw, last time i asked my teacher about the goto function and he said smthg like " we will have to work with goto function a couple of lessons more and after we introduce the loop like while/do/for, it will be forbidden to use the goto"
simply can't understand why we didn't manage to start with the loop but it is as it is...
Thanks again !
A finite state machine (its a lot to say, best google it) approach, similar but a little different. It will repeat to get more sums of new a,b,c as it resets the state after the result is printed, and to stop the cycle you have to choose x to exit. I am showing the approach, the prompts provided are minimal & you can paste your text over them.
if you wanted to get snarky you could enum-name the states rather than use magic numbers. If you wanted to go 'all in' you could look at the state to see what should be next and tailor the prompt to tell the user where they currently are. This is silly, of course: if you want a, then b, then c in that order, offering a choice is pointless... but sometimes you do what they tell ya. This is logically similar to againtry's code but it only has 1 tracker variable rather than a set of booleans, moving the complexity of several variables into complexity of several states.

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

int main() 
{
	int state = 0;
	menu:
	int a,b,c,r;
        char choice;
	cout << "enter a,b,c,r,x >";
	cin >> choice;
	switch(choice)
	{
		case 'a': if(state == 0)
		        {
					cout <<"enter a>";
					 cin >> a; 
					 state ++;
			}
  	            else 
					cout << "a already input\n"; 
				goto menu;
				
		case 'b': if(state == 1)
				{
					cout <<"enter b>";
					cin >> b; 
					state++;
				}
				else 
					cout << "b out of order\n"; 
				goto menu;
				
		case 'c': if(state == 2)
				{			
			         cout <<"enter c>";
					 cin >> c;
					 state++;
				}
				else 
					cout << "c out of order\n"; 
				goto menu;
		
		case 'r': if(state == 3) 
				{
					cout << "result:" << a+b+c << endl;
					state = 0;
				}
				else 
					cout << "sum not ready\n"; 
				goto menu;
		case 'x': goto done;
		default: cout << "junk\n"; goto menu;
	}
	done: ; 
}
Last edited on
Hello MaxGreen,

As I was looking back at what is required I noticed

I have to write a code using "switch", "if-else if" and "goto"


Do you really need if/else if/else or just if/else?

The code could be revised to use if/else if/else or just if/else if if needed.

I forgot to mention. Your code:
1
2
case 2: cout<<" Choose a value for b "<<endl;
                    cin>>b; cout<<" b= "<<b<<endl;

This is fine for the compiler, but is not easy to read. As a beginner it is best to make your code easy to read first before you do things that would be considered more advanced.

As for the "goto"s I figured it was something like what you said.


simply can't understand why we didn't manage to start with the loop but it is as it is


Because instantiations of learning do not always get it right and are slow to change. If they ever do. IMHO.

Andy
The trick is that the user have to type a, then b, then c, in order to get to calculate a+b-c and only afterwards to check the result.
In other words each of these steps are being a mandatory from 1 to 5.

If they have to do steps 1-5 in that order then there's no point to the switch statement. Just make them do each step in order.

I have to write a code using "switch", "if-else if" and "goto"
goto should always be a last resort. I don't think I've needed it in 15 years or more. It's unfortunate that you're being told you must use it.
Andy, I have to do my HW using these functions cause we only learned them. So last subject was the "switch" and what we finished already was "if-else if" and" goto" so I assume we can use only these functions.
Like my teacher said after he introduces the "while,do,for" loop, we never ever use the goto function, but idk why we've got familiar first with goto.

If they have to do steps 1-5 in that order then there's no point to the switch statement. Just make them do each step in order.

Thought the same, even if I don't have much experience in coding, as far as i understood switch is an option menu, so no need for each step to be a mandatory.
Does anyone know exactly how to write in c++ with if function smth. like " if value was not declared" ?
There appear to be too many people chiming in on this and causing a lot of unnecessary confusion for you. Most of what you had at the start is OK and just needs some steady thinking and fine adjustment.

The simple answer to your question is to use the bool test at the start of each switch case. If the test fails then the case breaks out back to the menu.

Just take each case (separately) at a time. Get the program to check if the required bool flags are set to true and proceed to do the calculation. If they aren't then break out back to the menu.

Stick with what you have and don't get side-tracked with all the bs going on here.

1
2
3
4
5
6
7
8
9
10
11
case 1:
            if(a_selected == false) // i.e value hasn't been previously entered
            {
                cout<<" Choose a value for a "<< endl;
                cin>>a;
                cout<<" a= "<<a<<endl;
                a_selected = true; // i.e it can't be re-entered
            }
            else
                goto menu;
            break;



@againtry thank you mate for all your help,
Let's see you do case 3.

This one requires a and b. But if you think about it you only need b because b requires a - if u get what I mean.
@againtry the problem is we didn't pass the boolean function..... :D :(
What do u mean by not passing the boolean function??
I have limited time left here but I have been tracking this so here is my tracking code which shows you how to move forward using the bool values.

Remember, this just tidies up your framework so it shouldn't appear complicated.
I decided to make it simpler for you by checking each bool at each case and printing out the values as you go.

You still have the last case to do. You can sort that one out.

Also there is an extra item to add to the menu to reset a,b and c bool's to false so new problem sets can be calculated.

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
92
93
94
95
96
97
#include <iostream>

using namespace std;

int main()
{
    int a{0}, b{0}, c{0}, R{0};
    int MENU;
    
    // EDIT small change
    bool a_selected = false;
    bool b_selected = false;
    bool c_selected = false; // <-- etc as required
    
menu:
    cout
    << "\n\n"
    << " MENU\n"
    << " 1. Enter a\n"
    << " 2. Enter b\n"
    << " 3. Enter c\n"
    << " 4. Calculate a+b-c\n"
    << " 5. Check the result\n\n"
    << " Choose from 1 to 5: ";
    
    cin >> MENU;
    
    switch(MENU)
    {
        case 1:
            if(a_selected == false and b_selected == false and c_selected == false)
            {
                cout << " Choose a value for a: ";
                cin >> a;
                cout << " a = " << a << '\n';
                a_selected = true;
            }
            else
                goto menu;
            break;
            
        case 2:
            if(a_selected == true and b_selected == false and c_selected == false)
            {
                cout << " Choose a value for b: ";
                cin >> b;
                cout << "a = " << a << " b = " << b << '\n';
                b_selected = true;
            }
            else
            {
                cout << "Enter a first\n";
                goto menu;
            }
            break;
            
        case 3:
            if(a_selected == true and b_selected == true and c_selected == false)
            {
                cout << " Choose a value for c: ";
                cin >> c;
                cout << "a = " << a << " b = " << b << " c = " << c << '\n';
                c_selected = true;
            }
            else
            {
                cout << "Enter a and/or b first\n";
                goto menu;
            }
            break;
            
        case 4:
            if (a_selected == true and b_selected == true and c_selected == true)
            {
                cout
                << " Calculate the result for a+b-c, which is equal to "
                << a+b-c << '\n';
            }
            else
                cout << "Choose a,b,c\n";
            break;
            
        case 5:
            if(R)
                cout << "Result is " << R << '\n';
            else
                cout<<"Choose option 4 to calculate the equation\n";
            break;
            
        default:
            cout<<"Impossible option, choose wisely\n";
            goto menu;
    }
    goto menu;
    
    return 0;
}
@againtry that's awesome and it works perfect for me. I am learning the boolean function by myself at the moment,
however my teacher will demand a code without the boolean function in it. As simple as possible, can't imagine how that's possible.

Once i will have his code option i will post it here , that will be on Monday afternoon.

Thanks again, you helped me anyway!
Last edited on
Well if you set a, b and c each to -90312 or some other ridiculous or un-permitted number then test for that each time instead of boolean. That way you don't need a_selected etc, just a{-90312}, b{... etc

So,

if (a != -90312 and b_selected != -90312 and c_selected == -90312) then go ahead and enter c.

Alternatively keep a_selected etc as int's and simulate true (1) false (0)

Where there's a will there's a way. :)

@againtry thank you pal, so much help from your side
Pages: 12