Big problem with classes , and returning from void function c++

Related searches

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 (){
    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 ;

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 ;;

appricate all of your wisom , thanks a lot

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:


Functions 2: Void (NonValue-Returning) Functions, Both value-returning functions and void functions receive values through their parameter lists. One example is the get function associated with the istream and ifstream classes: //void C requires variable declarations at the beginning of a block. This problem is particularly serious in environments where incorrect array� If the return type of a function is void the function doesn't return anything.So it makes little sense to have . Rational temp2 = temp.normalize(); With void functions, simply put them on a command of their own, without assignment.

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:


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;

Return from void functions in C++, The void function has returned a void() !!! The above code explains how void() can actually be useful to return void functions without giving error. I would be severly annoyed by such a pointless extra return statement at the end of a void function, since it clearly serves no purpose and just makes me feel the original programmer said "I was confused about this, and now you can be too!" which is not very nice.

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.

[Solved] Returning value from Void Function, Yes, you answered OP question, but why? But no, you're asking about returning something from a void function. With reference or pointer type a caution should be used: the object should exist even when an instance of the class is destroyed, Read also on the concept of l-value in any C++ manual. The lambda expression can be thought of as a nested function; returning from that doesn't return from the outer function, it returns from itself. [A function returning void] should technically always return NULL

Top 10 Most Common C++ Mistakes That Developers Make, void SomeMethod() { ClassA *a = new ClassA; SomeOtherMethod(); // it can throw an The big problem here is that C++ requires special delete operator with class B: public A { public: virtual std::string GetName() const {return "B";} }; void� Void as a Function Return Type . Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed. The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement.

Why use "return" at the end of "void" functions? - Talk, Hi, I realised that this is possibly the single most pointless question ever asked, but can't I just ask it anyway? Why do you 80% OFF ALL COURSES - FINAL WEEKS � GameDev. C allowed for subroutines by specifying the return type of “ void. Not a massive fan of using return as a control mechanism! Example 2: Pass and Return Object from the Function. In this program, the sum of complex numbers (object) is returned to the main() function and displayed.. #include <iostream> using namespace std; class Complex { private: int real; int imag; public: Complex(): real(0), imag(0) { } void readData() { cout << "Enter real and imaginary number respectively:"<<endl; cin >> real >> imag; } Complex

If the function has a void return type, this behavior is okay, but may be considered poor style. Use a plain return statement to make your intent clear. As a good engineering practice, always specify a return type for your functions. If a return value isn't required, declare the function to have void return type.

  • That's actually fine, as long as menu returns void.
  • @Quentin Ok, never encountered that before. Edited answer - Thanks.
  • my problem is not returning to menu . but exiting it///