0

I am currently making a program that has menu and sub menus and i'd like to have an opportunity to close entire thing from within any sub menu (which is inside function). I know i could do it different way (both way i have it now and way i know it could be done are below) but for future use (i could prove useful) i would like to know:

Is there a way to properly close the program from within a function ?

I've read a few answers here and here and from what i've gotten out of it is:

  1. I should not simply terminate the code
  2. I should not use any system specific code as to avoid compatibilty issues

A bit of clarifiacation - that is program for my personal educational purposes so it is kind of unnecessary to worry about it but i'd rather learn propper ways of dealing with those situations at the beginning than have to learn them anew once i discover how to do them later and have to go back and fix it.

As for code samples (using visual studio 2017):

//skipping unrelevant to question bits of code
                void sub_menu()
            {
                char a=0;
                system("cls"); //yet to get changed when i'll find other way of clearing screen
                std::cout << "Pick function:\n"
                    << "---------------------------------------------------------\n"
                    << "1.Foobar_1\n"
                    <<"---------------------------------------------------------\n"
                    << "ESC to go back to menu menu\n"
                    << "x to close";
                while (a != 27)
                {
                    a = _getch();
                    switch (a)
                    {
                    case 49:foobar_1(); break;
                    case 120:
                    case  88: system("exit"); break; //i'd like to replace that part
                    default: break;
                    };
                };
                return;
            }

void main()
            {
                char a=0;
                system("cls"); //yet to get changed when i'll find other way of clearing screen
                std::cout << "Pick what you want to do:\n"
                    << "---------------------------------------------------------\n"
                    << "1.Sub_menu\n"
                    << "2.foo\n"
                    << "3.bar\n"
                    <<"---------------------------------------------------------\n"
                    << "ESC to close\n"
                while (a != 27)
                {
                    a = _getch();
                    switch (a)
                    {
                    case 49:sub_menu(); break;
                    case 50:foo(); break;
                    case 51:bar(); break;
                    case 120:
                    case  88: system("exit"); break; //i'd like to replace that part
                    default: break;
                    };
                };
                return;
            }

The way i could implement it (i think it is merely byapssing the issue):

    //skipping unrelevant to question bits of code
       void main()
                {
                    char a=0;
                    system("cls"); //yet to get changed when i'll find other way of clearing screen
                    std::cout << "Pick what you want to do:\n"
                        << "---------------------------------------------------------\n"
                        << "1.Sub_menu\n"
                        << "2.foo\n"
                        << "3.bar\n"
                        <<"---------------------------------------------------------\n"
                        << "ESC to close\n"
                    while (a != 27)
                    {
                        a = _getch();
                        switch (a)
                        {
                        case 49:
{
{
                    char b=0;
                    system("cls");
                    std::cout << "Pick function:\n"
                        << "---------------------------------------------------------\n"
                        << "1.Foobar_1\n"
                        <<"---------------------------------------------------------\n"
                        << "ESC to go back to menu menu\n"
                        << "x to close";
                    while (b != 27)
                    {
                        b = _getch();
                        switch (b)
                        {
                        case 49:foobar_1(); break;
                        case 120:
                        case  88: a=27; break;
                        default: break;
                        };
                    };
                    return;
                }
}
; break;
                        case 50:foo(); break;
                        case 51:bar(); break;
                        case 120:
                        case  88: system("exit"); break; //i'd like to replace that part
                        default: break;
                        };
                    };
                    return;
                }
Ðаn
  • 10,934
  • 11
  • 59
  • 95
Marcin
  • 99
  • 1
  • 1
  • 7
  • 2
    exit() should always cleanly terminate your program, and, unlike system("exit") is part of the standard and platform independent. – Zinki Jun 09 '17 at 10:53
  • 1
    @Zinki there is a difference between *should* and *will*. Also you don't mention the responsible for this assumption: it's the programmer who *should* write code in a way that it can terminate at most points without requiring destructors to be called for resource release. – grek40 Jun 09 '17 at 10:57
  • @Zinki, `exit()` doesn't call most destructors in C++ code. It doesn't unwind the stack. I expect defensive programming to try to minimize the damage caused by an abrupt `exit()`, but it seems impractical to mandate that `exit()` should always cleanly terminate the process. – Eryk Sun Jun 09 '17 at 11:10
  • @zinki - i did not want to use it because of what @grek40 and @eryksun wrote. According to 1st topic i linked, using `exit()` causes program to behave like tourists on the beach - leaving all it's trash laying behind while leaving (pardon my weird comparison). But on the other hand it would be neat to have such convinience if it would finnish its job properly (as it does closing from within main). – Marcin Jun 10 '17 at 09:03

1 Answers1

0

This Code will work as you mentioned.

int main()
{
    int a;
    while (true)
    {
        system("cls"); //yet to get changed when i'll find other way of clearing screen
        cout << "Pick what you want to do:\n"
             << "---------------------------------------------------------\n"
             << "1.Sub_menu\n"
             << "2.foo\n"
             << "3.bar\n"
             << "---------------------------------------------------------\n"
             << "0. to close\n";
        cin >> a ;
        if (a == 0)
        {
            return 0 ;
        }
        else if (a == 1)
        {
            char b;
            while (true)
            {
                system("cls");
                cout << "Pick function:\n"
                     << "---------------------------------------------------------\n"
                     << "1.Foobar_1\n"
                     << "---------------------------------------------------------\n"
                     << "b to go back to menu menu\n"
                     << "x to close\n";
                cin >> b;
                if (b == '1')
                {
                    foobar_1();
                }
                else if (b == 'B' || b == 'b')
                {
                    break;
                }
                else if (b == 'x' || b == 'X')
                {
                    return 0;
                }
                else
                {
                    cout << "invalid input";
                }
            }
        }
        else if (a == 2)
        {
            foo();
        }
        else if (a == 3)
        {
            bar();
        }
        else
        {
            cout << "invalid input";
        }
    }
    return 0;
}
ArturFH
  • 1,697
  • 15
  • 28
  • OK then, i guess it is necessary to close the program from main then. Maybe i'll experiment a bit with additional global variable that if would be returned from function would close the loop and main would close as ordinary then. That seems a bit hacky though but maybe the only bad thing in this approach is my perspective on it. Nontheless thanks for advice. – Marcin Jun 10 '17 at 08:57
  • when you call a function then the main is in pause state and when you returned from a function then the state of main becomes un-paused.hope this answer will help you – Muhammad Adnan Ameer Jun 10 '17 at 11:36