main menu

hi. i know this program isnt does but i have a question. is there away like after the user clicks lets say 2 to find the factors of a number, but decides he or she wants to use the calculator instead. is there a way i can like let the user enter a number and it will bring them back to the choice of multiplication or multiplication table? - thanks

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

using namespace std;

int main()
{
    int choice;
    int whatnum;
    
    cout << "here are your options: " << endl << endl;
    cout << "1.Calculator" << endl;
    cout << "2.Multiplication table" << endl;
    cout << endl;
    cout << "enter the number before the operation: ";
    cin >> choice;
    
    switch(choice){
        
        case 1:
        break;
        case 2:
        cout << "please enter a number you want to find the factors of(up to 12): ";
        cin >> whatnum;
        }
}
Yes and my suggestion is to wrap what you need in a function. For example:

1
2
3
4
5
6
7
8
initialmenu(){
cout << "here are your options: " << endl << endl;
    cout << "1.Calculator" << endl;
    cout << "2.Multiplication table" << endl;
    cout << endl;
    cout << "enter the number before the operation: ";
    cin >> choice;
};


Now every time you need the main menu in some context you can call it and bring the user back to the beginning.

The switch is a great idea in this case of a menu so use it but heres the prototype:

1
2
3
function(condition){
                case [constant]:  //statements to execute if true
}


So your good up until case1 which has no statements which would have been as good as leaving it out. case2 is fine though, i'm assuming you'll add to it later.

Hope it helps
yah im going to fill in case 1 later. soon ill try what you told me but i also found another way. the goto loop. its working to go to the menu but how do i make it so like i can press 2 and continue on?

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

using namespace std;

int main()
{
    int choice;
    int whatnum;
    int mainmenu;
    loop:

    cout << "                   Main Menu" << endl << endl;

    cout << "here are your options: " << endl << endl;
    cout << "1.Calculator" << endl;
    cout << "2.Multiplication table" << endl;
    cout << endl;
    cin >> choice;

    switch(choice){

        case 1:
        break;
        case 2:
        cout << " press 1 to go back to the main menu or 2 to continue " << endl;
        cin >> mainmenu;
        if(mainmenu == 1) goto loop;
        cout << "please enter a number you want to find the multiple of(up to 12): " << endl;
        cin >> whatnum;
        switch(whatnum){
            case 2:
            cout << "the multiples of " << whatnum << "is 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 " << endl;
            cout << " 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100" << endl;
        }
        }
        cin.get();
        return 0;
}
I'm sorry but i dont understand. I compiled it and it seems to work just fine. i press 2 i continue, i press 1 and i go back to the main menu, i pressed 2 and it moves me forward through case 2, i pressed 2 again and it asked for factors which signals its working as you asked. So i dont understand at this point, maybe make it clear what exactly your looking for this program to do.
oh thanks i get it
Topic archived. No new replies allowed.