0

I'm still a newbie in programming, but I'm trying to make a program that's slightly larger and consists in way more functions than usual. And I want to make a repeatable 'Main menu' (which from you can access the rest of program's functions), but when I'm trying to call out the function again, nothing's happening. It looks like this:

void mainMenu()
{
    //clear console screen
    //menu of the program
    //i.e "Press 1 to choose something
    //console screen is cleared again, then new options appear
    //"Press E to go back to main menu"

    unsigned char v;
    v = getch();
    if (v == 'E')
        mainMenu();
}

What am I doing wrong? Shouldn't the mainMenu() be called out again, clear screen etc? I guess I could just return something from function which would cause the program to call mainMenu() again (and change mainMenu() to int for example), but there must be some workaround, which I'm missing.

tohava
  • 5,344
  • 1
  • 25
  • 47
cursmn
  • 3
  • 1

3 Answers3

2

You must add an option for exiting out of the loop too !

void mainMenu()
{
    system( "cls" );
    cout << "1. blah1\n2. blah2\n3. blah3\n4. Main menu\nE. Exit\n\n";
    unsigned char v = getch();

    if ( v == '1' )
    {
        cout << "blah1\n";
        // Call procedure for blah1
    }
    else if ( v == '2' )
    {
        cout << "blah2\n";
        // Call procedure for blah2
    }
    else if ( v == '3' )
    {
        cout << "blah3\n";
        // Call procedure for blah3
    }
    else if ( v == '4' )
    {
        mainMenu();
    }
    if ( v == 'E' )
    {
        return;
    }
}

int main()
{
    mainMenu();
}
Anmol Singh Jaggi
  • 8,376
  • 4
  • 36
  • 77
0

You mentioned that you are a newer to programming, but have you heard of the control structure in c++ called a switch/case statement? It might be suitable for the simple menu that you are trying to implement. You can read about it here.

A quick example in regards to your desired use case could look something like:

void mainMenu()
{
    unsigned char v, w;
    v = getch();
    switch(v)
    {
    case 'E':
        mainMenu();
        break;
    case 'A':
        w = getch();
        if (w == 1)
            callFunctionA();
        else
            mainMenu();
        break;
    case 'B':
        callFunctionB();
        break;
    // etc... You can have as many 'case' statements as you want depending on
    //        how many possibilities you want to handle on the user's input.
    default:
        break;
    }
}
alacy
  • 4,972
  • 8
  • 30
  • 47
  • @Smncru see my edit. However, there are much better ways to do this. Your modified use case (e.g. the one you just described in your comment) does not befit the use of a switch/case statement. In that case I would try a looping structure as demonstrated by zenith below. – alacy Mar 08 '15 at 13:46
0

I would recommend implementing your function this way:

void mainMenu()
{
    unsigned char v;
    do
    {
        //clear console screen
        //menu of the program
        //i.e "Press 1 to choose something
        //console screen is cleared again, then new options appear
        //"Press E to go back to main menu"

        v = getch();
    } while (v == 'E'); // Repeat do-while-loop if user entered 'E'
}

Now there's no change for stack overflow, because there's no recursion involved.

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157