Options after calculations are done?

This is a discussion on Options after calculations are done? within the C++ Programming forums, part of the General Programming Boards category; I was wondering how you could give the option to return to a main menu or to quit the program ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    Options after calculations are done?

    I was wondering how you could give the option to return to a main menu or to quit the program with just a Y or N input. I already know how to store a letter variable but I don't know how to use that variable to go back to the main menu or to quit the program.
    Thanks,
    bvsciguy

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Have you tried a loop around the program with the option as a condition?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    I have looked at loops in books and tutorials on the internet but I cannot figure out how to implement that in my code.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Bummer. So, were you going to post your code at any point so that we could actually help you?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        int a;
        cout << "Welcome to OSCaP. The Open Source Calculator Project.";
        cout << endl;
        cout << "What would you like to do today?";
        cout << endl;
        cout << "1. Add";
        cout << endl;
        cout << "2. Multiply";
        cout << endl;
        cout << "3. Subtract";
        cout << endl;
        cout << "4. Divide";
        cout << endl;
        cout << ">";
        cin >> a;
        
        if (a == 1)
        {
            int b, c;
            char z;
            cout << "What is the number that you would like to add to the second?";
            cout << endl;
            cin >> b;
            cout << "What is the number that you would like to add to the first?"\a;
            cout << endl;
            cin >> c;
            cout << b + c;
            cout << endl;
            cout << "Would you like to continue? 
        }   
        else if (a == 2)
        {
            int d, e;
            char x;
            cout << "What is the number that you would like to multiply by the second?";
            cout << endl;
            cin >> d;
            cout << "What is the number that you would like to multiply the first number by?";
            cout << endl;
            cin >> e;
            cout << d * e;
            cout << endl;
        }
        else if (a == 3)
        {
            int f, g;
            char y;
            cout << "What is the number that you would like to subtract by the second?";
            cout << endl;
            cin >> f;
            cout << "What is the number that you would like to subtract eh first by?";
            cout << endl;
            cin >> g;
            cout << f - g;
            cout << endl;
        }
        else if (a == 4)
        {
            int h, i;
            char u;
            cout << "What is the number that you would like to divide by the second?";
            cout << endl;
            cin >> h;
            cout << "What is the number that you would like to divide the first number by";
            cout << endl;
            cin >> i;
            cout << h / i;
            cout << endl;
        }  
        system ("PAUSE");
        return 0;
    }
    I just don't know the command required to go back to start.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    That code doesn't compile. This does, and it adds a loop. You're welcome.
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      while ( 1 ) {
        int a;
        cout << "Welcome to OSCaP. The Open Source Calculator Project.";
        cout << endl;
        cout << "What would you like to do today?";
        cout << endl;
        cout << "1. Add";
        cout << endl;
        cout << "2. Multiply";
        cout << endl;
        cout << "3. Subtract";
        cout << endl;
        cout << "4. Divide";
        cout << endl;
        cout << "5. Quit";
        cout << endl;
        cout << ">";
        cin >> a;
    
        if (a == 1)
        {
          int b, c;
          cout << "What is the number that you would like to add to the second?";
          cout << endl;
          cin >> b;
          cout << "What is the number that you would like to add to the first?\a";
          cout << endl;
          cin >> c;
          cout << b + c;
          cout << endl;
          cout << "Would you like to continue? ";
        }   
        else if (a == 2)
        {
          int d, e;
          cout << "What is the number that you would like to multiply by the second?";
          cout << endl;
          cin >> d;
          cout << "What is the number that you would like to multiply the first number by?";
          cout << endl;
          cin >> e;
          cout << d * e;
          cout << endl;
        }
        else if (a == 3)
        {
          int f, g;
          cout << "What is the number that you would like to subtract by the second?";
          cout << endl;
          cin >> f;
          cout << "What is the number that you would like to subtract eh first by?";
          cout << endl;
          cin >> g;
          cout << f - g;
          cout << endl;
        }
        else if (a == 4)
        {
          int h, i;
          cout << "What is the number that you would like to divide by the second?";
          cout << endl;
          cin >> h;
          cout << "What is the number that you would like to divide the first number by";
          cout << endl;
          cin >> i;
          cout << h / i;
          cout << endl;
        }
        else if (a == 5)
        {
          break;
        }
      }
      system ("PAUSE");
      return 0;
    }
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    Thank you very much. I will use this a learning experience.

  8. #8
    Registered User LiNeAr's Avatar
    Join Date
    Aug 2005
    Posts
    31
    Or you could just use a switch case statement. They aren't to hard, if you don't know what it is, there is a tutorial on this site.

    Example:

    Code:
    case 1:
             cout << "HELLO";
             break;
    case 2:
             cout << "GOODBYE";
             break;
    Or with letters:

    Code:
    case 'Y':
               cout << "WELCOME";
               break;
    case 'N':
               cout << "BYE";
               break;
    They are both good ways, I just prefer the switch statement.
    IDE: Microsoft Visual C++ .net Standard 2003

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 01:24 PM
  2. What's the best way to handle many program options?
    By stickmangumby in forum C Programming
    Replies: 19
    Last Post: 06-06-2007, 04:06 PM
  3. C program compile but not doing the calculations
    By abs.emailverify in forum C Programming
    Replies: 8
    Last Post: 11-08-2006, 07:43 AM
  4. How do I get these calculations correct?
    By nadeni0119 in forum C++ Programming
    Replies: 10
    Last Post: 04-07-2003, 11:09 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21