main menu and sub menu

Hey. I'm trying to have two menu's in this program,a main menu and multiple sub menu's.. but the thing is i'm trying to fit in a "return to the main menu" option, i have alot of ideas that didn't seem to work.. any solutions fellas ?

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
  #include <iostream>
using namespace std;

char menu(char c);

int triangle(int rows);

int main()
{
    char choice,choose;
    int x;
    choose=menu(choose);
    if(choose=='A')
    {
      //do something
    }
    else
    if(choose=='B')
    {
      //do something
    }
    else
    if(choose=='C')
    {
     cout<<"1- Draw Triangle "<<endl;
     cout<<"2- Return to the main menu"<<endl;
     cin>>choice;
     if(choice == '1')
     {
     cout<<"please enter number of rows "<<endl;
     cin>>x;
     triangle(x);
     }
     if(choice == '2')
     //return to main menu
    
    }
    else
    if(choose=='D')
    exit;
    
    return 0;
}

int triangle(int rows)
{
    for(int i=rows;i>=1;i--)
    {
        for(int k=1;k<=i;k++)
        {
            cout<<"*";
        }
        cout<<endl;
    }
}

char menu(char c) 
{
    cout<<"A or a. 1D Array."<<endl;
    cout<<"B or b. 2D Array."<<endl;
    cout<<"C or c. Draw using recursion."<<endl;
    cout<<"D or d. Exit"<<endl;
    cin>>c;
    return c;
}
you probably want a while loop: https://en.cppreference.com/w/cpp/language/while
Play around with something like this...

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
 #include <iostream>
 
using namespace std;

char menu(char c);

int triangle(int rows);

int main() {
    
    char choice,choose;
    int x;
    bool b = true;
    
    while(b == true) {
    do {
    choose=menu(choose);
    choose = tolower(choose);}
    
    while(choose!='a' && choose!='b' && choose!='c' && choose != 'd');
    
    if(choose=='a')
    {
      //do something
    }
    else
    if(choose=='b')
    {
      //do something
    }
    else
    if(choose=='c')
    {
      cout<<"1- Draw Triangle "<<endl;
      cout<<"2- Return to the main menu"<<endl;
      cin>>choice;
      if(choice == '1')
      {
      cout<<"please enter number of rows "<<endl;
      cin>>x;
      triangle(x);
      }
     if(choice == '2') 
     {
     //return to main menu
    
      }
    }
    else
    if(choose=='d'){
    b = false;
    }
}
return 0;
}

int triangle(int rows)
{
    for(int i=rows;i>=1;i--)
    {
        for(int k=1;k<=i;k++)
        {
            cout<<"*";
        }
        cout<<endl;
    }
}

char menu(char c) 
{
    cout<<"A or a. 1D Array."<<endl;
    cout<<"B or b. 2D Array."<<endl;
    cout<<"C or c. Draw using recursion."<<endl;
    cout<<"D or d. Exit"<<endl;
    cin>>c;
    return c;
}
Hello FoaaD2506,

You should compile the program and try to fix all the errors that you can first. The errors that you can not fix make note of them in your post.

In "main" you should initialize your variables when you define them. "choice" was giving me an error being uninitialized.

"else if" statements are generally written as else if (choose == 'B'). Otherwise your indenting is off an it makes it harder to follow.

Given the line choose=menu(choose); I added:
1
2
3
choose = menu(choose);

choose = std::toupper(choose);  // <--- Added. Requires header file "cctype". 

The other option would be if (choose == 'A' || choice == 'a') to check for either case.

in the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
else if (choose == 'C')
{
    cout << "1- Draw Triangle " << endl;
    cout << "2- Return to the main menu" << endl;
    cin >> choice;

    if (choice == '1')
    {
        cout << "please enter number of rows " << endl;
        cin >> x;

        triangle(x);
    }
    else if (choice == '2')
        ;  // <--- Place holder for now.
    //    //return to main menu
}
else if (choose == 'D')
    ;  // <--- Place holder for now.  

For line 15 you use "exit", but exit is a function and needs to be "exit()", but you should not end the program with this. It may not clean up everything before the program ends.

For now I had to use a (;) to clear up the errors and get the program to compile.

Using your code. Starting with line 12 and ending with line 40 put all of that in a do/while loop with the while condition while (choose != 'D'); and you will stay in the loop until you choose "D" to exit.

The function "triangle" promises to return an int, but the function does not return anything. Did you have a reason for returning an "int"? Otherwise this should be a "void" function since I see nothing to return.

A suggestion. The if/else if statements could easily be put in a switch and each case should call a function instead of being the program. It is better to let "main" control the program flow and not be the program.

I need a little time to rework the "menu" function. It is a different approach that you may find useful.

Andy
Hello FoaaD2506,

This is what I like to do with a "menu" function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char MainMenu()
{
    char choice{};

    do
    {
        cout <<
            "\n A or a. 1D Array."
            "\n B or b. 2D Array."
            "\n C or c. Draw using recursion."
            "\n D or d. Exit"
            "\n Enter choice: ";
        cin >> choice;

        choice = std::toupper(choice);

        if (choice < 'A' || choice > 'D')
            std::cout << "\n     Invalid choice! try again.\n";
    } while (choice < 'A' || choice > 'D');

    return choice;
}

I like to call the function "MainMenu". This allows you to write other functions that end with "Menu", but start with something that describes what the menu is for.

There is no need to send a variable to the function because you are returning the outcome of the function back to "main". Also the variable you are sending to the function is just a copy of what is in "main". A different way would be void MainMenu(char& choose) this way when you leave the do/while loop "choose" in "main" will have the correct value. Also you will not need the "return" statement.

If you use this suggestion be sure to change the prototype and function call.

Notice the use of the "\n"s. "endl" will flush the output buffer, but this comes with overhead and to many "endl"s could slow the program. Not that something this small would show any difference.

Another point that helps is following a "cout" with a "cin". The "cin" will flush the output buffer before it allows any input. An advantage that you can make use of.

With the if statement and while condition you only return a valid choice.

Andy
Hello Andy/Manga.
first you were right about triangle function it was a typo from me i edited it.
second- i seem to not understand why the 'while' loop not working for me..
here take a look
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
int main()
{
    char choice,choose;
    int x;
    choose=menu(choose);
    while(choose != 'D' || choose=='d')
    {
    if(choose=='A' || choose=='a')
    {
     while(choice != '4')   
     {
    cout<<"1-do something"<<endl;
    cout<<"2-do something"<<endl;
    cout<<"3-do something"<<endl;
    cout<<"4-Return to the main menu."<<endl;
    cin>>choice;
     }
    }
    else
    if(choose=='B' || choose=='b')
    {
     
    }
    else
    
    if(choose=='C' || choose=='c')
    {
    while(choice != '2')
     {
     cout<<"1- Draw Triangle "<<endl;
     cout<<"2- Return to the main menu"<<endl;
     cin>>choice;
     if(choice == '1')
     {
     cout<<"please enter number of rows "<<endl;
     cin>>x;
     triangle(x);
     }
     }
    }
    
    }
    return 0;

}

i think it's all right what i'm trying to do here .. while choice is NOT 2 keep re-displaying the sub menu but when i enter '2' nothing happens .. it should break out to the main menu right ?
what am i doing wrong ?
Last edited on
Hello FoaaD2506,

Going over your program. Some things I have changed:
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
#include <iostream>

using namespace std;

char menu(char c);  // <--- Do not need to send anything to the function. The value returned from the function takes case of this.
void triangle(int rows);

int main()
{
    char choice{}, choose{};
    int x{};

    choose = menu(choose);
 
    while (choose != 'D' && choose != 'd')
    {
        if (choose == 'A' || choose == 'a')
        {
            while (choice != '4')
            {
                cout << "1-do something" << endl;
                cout << "2-do something" << endl;
                cout << "3-do something" << endl;
                cout << "4-Return to the main menu." << endl;
                cin >> choice;
            }
        }
        else
            if (choose == 'B' || choose == 'b')
            {

            }
            else
                if (choose == 'C' || choose == 'c')
                {
                    while (choice != '2')
                    {
                        cout << "1- Draw Triangle " << endl;
                        cout << "2- Return to the main menu" << endl;
                        cin >> choice;

                        if (choice == '1')
                        {
                            cout << "please enter number of rows " << endl;
                            cin >> x;

                            triangle(x);
                        }
                    }
                }
    }

    return 0;
}

void triangle(int rows)
{
    for (int i = rows; i >= 1; i--)
    {
        for (int k = 1; k <= i; k++)
        {
            cout << "*";
        }
        cout << endl;
    }
}

char menu(char c)
{
    cout << "A or a. 1D Array." << endl;
    cout << "B or b. 2D Array." << endl;
    cout << "C or c. Draw a Triangle." << endl;  // <--- This is not a recursive function.
    cout << "D or d. Exit" << endl;
    cin >> c;
    return c;
}

You need to change line 5 to char menu(); along with the function call and function definition.

Inside "main" you need to initialize your variables.
1
2
char choice{}, choose{};
int x{};  // <--- "x" should be called "rows". Try to avoid single letter variables. 

The "uniform initializer" the empty {}s will set the "char"s to "\0" and the "int" to (0) zero. This is needed later on in the program.

Line 13 should be inside the while loop not outside. Also having initialized the variable "choose" the condition will evaluate to true.

In line 15 the while condition should be a logical AND (&&). My experience has bee when using 2 "!="s the (&&) works better most of the time.

Choice "A" and "B" I have not worked on yet.

In choice "C":
Line 36. First initializing the variable "choice" allow you to enter the while loop because it is not equal to '2'. By not initializing the variable it gave me a run time error saying "that the variable was being used uninitialized". This while condition allows for any ASCII character other than '2' to be a valid choice, but when you do enter a choice you never check if it is a valid choice.

When you finish the "if" statement for "C" you go back to the first while loop, but since "choose" never changes you have an endless loop with no way out. That is where moving line 13 inside the while loop is needed.

After line 40 you should check that what was entered is a (1) or a (2). Although the while loop keeps repeating you never know why.

Your code:
1
2
3
cout << "1- Draw Triangle " << endl;
cout << "2- Return to the main menu" << endl;
cin >> choice;

The code works, but I think you would be better with:
1
2
3
4
5
cout <<
    "\n1- Draw Triangle "
    "\n2- Return to the main menu"
    "\nEnter choice: ";
cin >> choice;

This is a little better representation of what will be displayed on the screen and it is easier to maintain or change.

After "main" I just used your original functions just to test the program.

Andy
Topic archived. No new replies allowed.