Thread: Returning to main menu

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    10

    Returning to main menu

    On my calculator, it asks the user what they want to do.(switch/case functions)
    After the person adds, subtracts, multiplys or divides the 2 numbers, the calculator exits. Instead of using return 0 and having it exit, how can I have it return to the main menu?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You can use a while loop, and then the last menu item can be something like "Exit". If the person types in the menu number corresponding to exit, then make the while loop terminate.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    10
    Thank you. I will see what I can do.

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    I did something very similar a very long time ago..... (hence not using the standard and all that stuff) help this helps a bit. You may need to add an extra case statement for an exit option or something.

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    void MenuHeader(char Buffer[],int BufferLength);
    
    int main()
    {
      const int MAX_BUFFER=512;
    
      char Buffer[MAX_BUFFER];
      short Answer,Num1,Num2,Quotient,Remainder;
      int MenuNum;
    
      do
      {
        MenuHeader(Buffer,MAX_BUFFER);
        MenuNum = atoi(Buffer);
    
        switch(MenuNum)
        {
          case 1: GetNumbers(Num1,Num2,MenuNum);
                  Addition(Num1,Num2,Answer);
                  DisplayResults(Num1,Num2,Answer,0,0);
                  break;
          case 2: GetNumbers(Num1,Num2,MenuNum);
                  Subtraction(Num1,Num2,Answer);
                  DisplayResults(Num1,Num2,Answer,0,0);
                  break;
          case 3: GetNumbers(Num1,Num2,MenuNum);
                  Multiply(Num1,Num2,Answer);
                  DisplayResults(Num1,Num2,Answer,0,0);
                  break;
          case 4: GetNumbers(Num1,Num2,MenuNum);
                  Division(Num1,Num2,Quotient,Remainder);
                  DisplayResults(Num1,Num2,Quotient,Remainder,MenuNum);
                  break;
          case 5: GetNumbers(Num1,Num2,MenuNum);
                  Powers(Num1,Num2);
                  break;
          case 6: GetNumbers(Num1,Num2,MenuNum);
                  EvenNumbers(Num1);
                  break;
          case 7: GetNumbers(Num1,Num2,MenuNum);
                  ShiftLeft(Num1,Answer);
                  DisplayResults(Num1,Num2,Answer,0,0);
                  break;
          case 8: GetNumbers(Num1,Num2,MenuNum);
                  DecBinary(Num1);
                  break;
          case 9: GetNumbers(Num1,Num2,MenuNum);
                  Cubes(Num1);
        }
      }while(MenuNum != 10);
    
      return 0;
    }
    
    void MenuHeader(char Buffer[],int BufferLength)
    {
      clrscr();
      cout << "******************************Assembler Calculator******************"
           <<"************\n1. Addition\n2. Subtraction\n3. Multiply\n4. Divide\n"
           << "5. Powers\n6. Even Numbers\n7. Shift Left\n8. Decimal to Binary\n"
           << "9. Cubes\n10. Quit\n\nWhere do you want to go today? ";
      cin.clear();
      cin.get(Buffer,BufferLength,'\n');
      cin.ignore();
    }
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:54 AM
  2. Returning to main.
    By Deltahawk12 in forum C++ Programming
    Replies: 11
    Last Post: 09-10-2005, 09:11 PM
  3. Main Declaration error
    By starkhorn in forum C++ Programming
    Replies: 11
    Last Post: 06-22-2005, 09:04 AM
  4. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM
  5. quick question about C Dos text menu pgm i was doing
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 09-16-2001, 10:26 AM