Thread: Wont return to menu() function

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    Wont return to menu() function

    So I've got another problem with a program. It's not really that big of a deal, but I can't seem to figure out why it's not working. I searched, but I couldn't find anything related to this. I've got a geometry calculation program, you decide which shape you want to calculate for, and then the actual calculation. Then you input the values necesary for the calculation. The program calculates, and outputs the answer. Finally it returns to the menu, incase you need to do more calculations. Everything works but returning to the menu() function, and I can't figure out why.
    Any help is greatly appreciated. Thanks!


    Code:
    #include <iostream>
    #include <string>
    #include <math.h>
    
    //Funtion prototypes.
    void menu();
    int which_calc();
    void calc_circle();
    void calc_rectangle();
    void calc_triangle();
    
    using namespace std;
    
    int main()
    {
        menu();
        system("pause");
        return (0);
    }
    
    void menu()
    {
        int choice = 0;
        do
        {
            cout << "Which formula would you like to use?\n\n";
            cout << "   1.  Calculate for a circle\n";
            cout << "   2.  Calculate for a rectangle\n";
            cout << "   3.  Calculate for a triangle\n";
            cout << "   4.  Quit\n";
            cin >> choice;
            if (choice < 1 || choice > 4)
               cout << "Please enter a correct menu number\n";
        }while (choice < 1 || choice > 4);
        
        switch(choice)
        {
             case 1: cout << "You chose calculate for a circle\n\n";
                     calc_circle();
                     break;
             case 2: cout << "You chose calculate for a rectangle\n\n";
                     calc_rectangle();
                     break;
             case 3: cout << "You chose calculate for a triangle\n\n";
                     calc_triangle();
                     break;
             case 4:
                     break;    
        }//!End of switch/case
        return;
    }
    
    /*
    Function whichCalc() gets rid of several lines of repeating code
    by asking which calculation they want and returning int choice.
    */
    int which_calc()
    {
        int choice = 0;
        do
         {
             cout << "Which calculation would you like?\n\n";
             cout << "   1. Circumference\n";
             cout << "   2. Area\n";
             cin >> choice;
             if (choice < 1 || choice > 2)
             {
               cout << "Please enter a correct menu number\n";
             }
         }while(choice < 1 || choice > 2);
         return(choice);
    }
    
    void calc_circle()
    {
         int choice = which_calc();
         double radius, area, circumference;
         const double PI = 3.141592654;
         
         cout << "Please enter radius: ";
         cin >> radius;
         
         if (choice == 1)
         {
             cout << "The circumference of a circle with a radius of " << radius 
                  << " is " << (2 * PI * radius) << endl;
         }
         else
         {
             cout << "The area of a circle with a radius of " << radius 
                  << " is " << (PI * PI * radius) << endl;
         }
         menu();  //This is where I want it to return to the original menu.
    }

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Maybe callee can't call its caller. Make a loop in menu(). Run your program in debug mode.

    [EDIT]
    In your code functions actually don't return to menu(), they call it.
    Last edited by siavoshkc; 09-04-2006 at 04:47 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    What's the compiler you are using? Your code works as you expected with MinGW 3.4.5.

    The only change I had to make was to comment the calls to calc_triangle() and calc_rectangle() since these functions weren't defined. But tis has no effect on your problem.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The OP wants to call menu infinitely, I think.

    Try something like this:
    Code:
    // in main()
    while(!menu());
    and make menu() return 0 to continue looping, 1 to quit.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by System_159
    Code:
         menu();  //This is where I want it to return to the original menu.
    It actually calls menu() and as such will only leave when the option chosen is 4. So i'm a little baffled.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well what if you wanted to calculate multiple triangles, then where would you be?

    I think the OP's looking for a sort of loop around the existing code, which could be implemented as a loop in menu() or as a loop in main() based on the return value of menu().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Well, true.

    I've been assuming he has the other functions defined. As is, the code will give a link-time error.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Read the original post.
    I've got a geometry calculation program, you decide which shape you want to calculate for, and then the actual calculation. Then you input the values necesary for the calculation. The program calculates, and outputs the answer. Finally it returns to the menu, incase you need to do more calculations. Everything works but returning to the menu() function, and I can't figure out why.
    It definitely sounds like it compiled. It sounds a lot like they just need to call menu() again.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you continue to run that code it will result in a stack overflow since the functions being called never return and menu never actually returns, thus the old stack frames are still in effect.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    dwks is right. I just want to have the menu reappear until the user inputs "4" to quit.
    I tried using return(menu()); at the end of the circle function. I also changed the actual menu function to return the corresponding function instead of just calling it. Then I tried clearing the input at the beginning of menu(). None of that worked though.

    Try something like this:

    Code:
    // in main()
    while(!menu());
    and make menu() return 0 to continue looping, 1 to quit.
    I'm not sure what toyu mean by this dwks

    Thanks for all the ideas so far, though.

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Code:
    void menu()
    {
        for(;;){
            int choice = 0;
            do
            {
                cout << "Which formula would you like to use?\n\n";
                cout << "   1.  Calculate for a circle\n";
                cout << "   2.  Calculate for a rectangle\n";
                cout << "   3.  Calculate for a triangle\n";
                cout << "   4.  Quit\n";
                cin >> choice;
                if (choice < 1 || choice > 4)
                   cout << "Please enter a correct menu number\n";
            }while (choice < 1 || choice > 4);
        
            switch(choice)
            {
                    case 1: cout << "You chose calculate for a circle\n\n";
                           calc_circle();
                           break;
                    case 2: cout << "You chose calculate for a rectangle\n\n";
                  	     calc_rectangle();
                           break;
                    case 3: cout << "You chose calculate for a triangle\n\n";
                	     calc_triangle();
                           break;
                    case 4:
                          return;    
        	   }//!End of switch/case
        }
        return;
    }
    And erase menu() at the end of calculation functions.
    Last edited by siavoshkc; 09-05-2006 at 09:14 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And erase menu() at the end of calculation functions.
    This is extremely important.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    My fix was the following:
    Code:
    #include <iostream>
    #include <string>
    #include <math.h>
    
    //Funtion prototypes.
    int menu();
    int which_calc();
    void calc_circle();
    void calc_rectangle();
    void calc_triangle();
    
    using namespace std;
    
    int main()
    {
        // while(menu()) {} // edited
        while(!menu()) {}
        system("pause");
        return (0);
    }
    
    int menu()
    {
        int choice = 0;
        do
        {
            cout << "Which formula would you like to use?\n\n";
            cout << "   1.  Calculate for a circle\n";
            cout << "   2.  Calculate for a rectangle\n";
            cout << "   3.  Calculate for a triangle\n";
            cout << "   4.  Quit\n";
            cin >> choice;
            if (choice < 1 || choice > 4)
               cout << "Please enter a correct menu number\n";
        }while (choice < 1 || choice > 4);
        
        switch(choice)
        {
             case 1: cout << "You chose calculate for a circle\n\n";
                     calc_circle();
                     break;
             case 2: cout << "You chose calculate for a rectangle\n\n";
                     calc_rectangle();
                     break;
             case 3: cout << "You chose calculate for a triangle\n\n";
                     calc_triangle();
                     break;
             case 4:
                     return 1;
        }//!End of switch/case
        return 0;
    }
    
    /* . . . same code as above . . . */
    Blue lines are changed, the actual changes are in bold.
    Last edited by dwks; 09-06-2006 at 09:16 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think your while loop does the opposite of what you want, dwks. Returning 0 will break the loop, returning 1 will continue it.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, indeed. I meant to put while(!menu()) as I originally indicated.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM