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,207
  • 1
  • 18
  • 47
cursmn
  • 3
  • 1
  • There is nothing wrong in this code – Steephen Mar 08 '15 at 13:28
  • How do you know mainMenu is not called? – tohava Mar 08 '15 at 13:30
  • recursion, can be fine. My concern here is that you'll have a stack overflow and your application will terminate. – Mgetz Mar 08 '15 at 13:30
  • Yet it is not working as intended. Is it because I'm using an argument? My code seems to ignore mainMenu(args here) and exits. – cursmn Mar 08 '15 at 13:33
  • `mainMenu()` doesn't accept any args – tohava Mar 08 '15 at 13:35
  • Which is because in main() after my first mainMenu() callout there's a getch(); and return 0; so it's not recalling, it's just going back to main() and exits. – cursmn Mar 08 '15 at 13:36
  • I would not recommend recursion in this case, [here's why](http://stackoverflow.com/questions/3021/what-is-recursion-and-when-should-i-use-it). – emlai Mar 08 '15 at 13:36

4 Answers4

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
  • 7,859
  • 3
  • 36
  • 72
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,712
  • 7
  • 26
  • 45
  • Yeah, but my main menu looks like this: "Choose A, B or C -> if you chose A: -> Choose now 1, 2, 3 or go back to step 1 (Choose A B or C). " Is it not possible to make it this way without splitting it into more functions? – cursmn Mar 08 '15 at 13:41
  • @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
emlai
  • 39,703
  • 9
  • 98
  • 145
0

This is so weird, but I didn't use 'else' between all my ifs as Anmol posted.

unsigned char klik4;
            do
            {
                klik4 = getch();
                if(klik4 == '1');
                    //WSTAWIC FUNKCJE
                else if(klik4 == '2');
                    //WSTAWIC FUNKCJE
                else if(klik4 == '3');
                    //WSTAWIC FUNKCJE
                else if(klik4 == '4');
                    //WSTAWIC FUNKCJE
                else if(klik4 == '5');
                    //WSTAWIC FUNKCJE
                else if(klik4 == '6');
                    //WSTAWIC FUNKCJE
                else if(klik4 == '7');
                    //WSTAWIC FUNKCJE
                else if(klik4 == '8')
                    {
                    mainMenu();
                    return 0;
                    }

            }while(klik4 < 49 || klik4 > 56);

That's an extract from my code, and now it's calling mainMenu() properly (Though I'm not sure if return is necessary there(my function is now int, not void)). Previously I wasn't using elses, I just thought they're not necessary, but it seems that was the problem. I don't really see why, my problem is resolved, but if somebody could explain why was that happening, I'd be grateful.

cursmn
  • 3
  • 1