0

so that's my first time learning a language , and I was really excited to play with classes, i do have one major problem which i cant seem to understand ; im building a bank menu . its a class ofcourse, and i have a different class of clients which is basicly an array in my bank. so my menu function inside the bank looks like that :

 void menu (){
    manager();
    int n,m;
    cout << "welcome to our bank managment system. \n";
    cout << "please choose one of the following options : \n";
    cout << "1-add a new client\n";
    cout << "2-remove a leaving client\n";
    cout << "3-bank statistics\n";
    cout << "4-if you are a costumer\n";
    cout << "5-exit\n";
    cin  >> n ;
    if()
    if()
    if()
    if()
    if()

note that my return function is been summoned a lot inside

i have a return function to go back to the menu :

void returnfunction (){
    int back = 0;
    cout << "\n0-back to menu \n press a diffrent number back to exit :)\n";
    cin >> back ;
     if (back==0){
        return menu();
     }
    if (back!=0){
    cout << "thank you for using our program ! ";
       return ;
    }

it is working perfect until i play with it to much , and then hit 5 to exit (that's my option for n==5) i must emphasize that when im hitting 5 only after few simple actions its working fine... how can i fix that return function ?

ofcourse my main looks like that :

int main()
{
    srand (time(NULL));
    Bank b ;
    b.menu();
}

appricate all of your wisom , thanks a lot

Q Wolf
  • 43
  • 5

3 Answers3

0

Your function:

void returnfunction ()

is declared to return nothing (void) but you:

   return menu();

do return something, that's very unclear (even though menu() returns void too)

If you want to call menu() and then return write:

    menu();
    return;
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

There are a couple problems with this code, and honestly it wouldn't compile in other imperative OO languages. But this is c++ where the rules don't matter. Aside: If you don't have a strong reason to be using C++, learn Rust first. I promise you'll thank me later.

Paul has the right of it. The compiler should error out at that statement:

return menu();

However the equivalent is perfectly legal:

menu();
return;

But this still will cause problems in theory (but maybe not in practice) because your function is almost, but not, a candidate for tail recursion optimisation. More here Which, if any, C++ compilers do tail-recursion optimization?

This becomes a problem when users return to the menu many times, it depletes your programs memory, eventually leading to a stack overflow fault. The common pattern you'll find in most every GUI / Graphics library is that of a main-loop. Something like:

int main() {
  bool exit = false
  while(!exit) {
    int action = menu()
    switch(action) {
    case EXIT_SELECTION:  exit = true; break;
    case SHOW_STATISTICS: printStats(); break;
    }
  }
}
Keynan
  • 1,338
  • 1
  • 10
  • 18
0

Each time you call a function, your program has to use more memory to keep track of everything related to that function call. Ordinarily this memory is released once a function ends, but because your menu function calls another function that calls your menu function that calls another function... and on and on, you will eventually run out of memory from all of the function calls since these functions cannot terminate until the functions they call terminates -- and thus your program will crash. The solution is to use a while loop and check the user's input for an exit code as a previous responder mentioned. It can look something like:

`void menu() {
    char choice= '\0';

    while(choice!= 3) {
        std::cout << "Welcome to the menu!";
        std::cout << "\t Option 1 \n";
        std::cout << "\t Option 2 \n";
        std::cout << "\t Option 3 \n";
        std::cout << "Your option: ";
        std::cin >> choice;

        if(choice == 1) { /*do something*/ }
        else if(choice == 2) { /*do something else*/ }
        else if(choice == 3) { /*print a message and exit*/ }
        else { /*bad choice -- try again*/ }
    } //end while-loop
} //end menu()`

Also, notice that your functions' return types are void, which, by definition, cannot have any sort of return. C++ will allow you to say return; inside of a void function, but it is merely a way to escape the function right then and there and is not really intended to do anything more than that. Using return in any other way when working with void functions is confusing and runs a risk of causing big issues.

Shaavin
  • 125
  • 8