Thread: How to add a Menu?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    How to add a Menu?

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
        int number;
        double num1;
        double num2;
        double num3;
        char goback;
        cout << "Type in 1 to use addition\n" << endl << "Type in 2 to use subtraction\n" << endl << "Type in 3 to use division\n" << endl << "Type in 4 to use multiplication\n" << endl << "Type in 5 to find the square root of a number\n" << endl << "Type in 6 to square a number\n" << endl;
        cin >>  number;
        if ( number == 1 ) {
            cout << "Please enter your first number\n" << endl;
            cin >> num1;
            cout << "Please enter your second number\n" << endl;
            cin >> num2;
            num3 = num1 + num2;
            cout << "The sum of your two numbers is " << num3 << " Press b to go back to the main menu."<< endl;
            cin >> goback;
            cin.get();
        }
        else if (number == 2) {
            cout << "Please enter your first number\n" << endl;
            cin >> num1;
            cout << "Please enter your second number\n" << endl;
            cin >> num2;
            num3 = num1 - num2;
            cout << "The difference between your numbers is " << num3 << " Press b to go back to the main menu."<< endl;
            cin >> goback;
            cin.get();
        }
        else if (number == 3 ) {
            cout << "Please enter your first number\n" << endl;
            cin >> num1;
            cout << "Please enter your second number\n" << endl;
            cin >> num2;
            num3 = num1 / num2;
            cout << "The quotient of your numbers is " << num3 << " Press b to go back to the main menu."<< endl;
            cin >> goback;
            cin.get();
        }
        else if (number == 4 ) {
            cout << "Please enter your first number\n" << endl;
            cin >> num1;
            cout << "Please enter your second number\n" << endl;
            cin >> num2;
            num3 = num1 * num2;
            cout << "The product between your numbers is " << num3 << " Press b to go back to the main menu."<< endl;
            cin >> goback;
            cin.get();
        }
        else if (number == 5 ) {
            cout << "Please enter your number\n" << endl;
            cin >> num1;
            num2 = sqrt(num1);
            cout << "The square root of your number is " << num2 << " Press b to go back to the main menu."<< endl;
            cin >> goback;
            cin.get();
        }
        else if (number == 6) {
            cout << "Please enter your number\n" << endl;
            cin >> num1;
            num2 = pow(num1,2.0);
            cout << "Your number squared is " << num2 << " Press b to go back to the main menu."<< endl;
            cin >> goback;
            cin.get();
        }
        else {
            cout << "Invalid number\n" << " Press b to go back to the main menu." <<endl;
            cin >> goback;
            cin.get();
        }
        cin.get();
        return 0;
    
    }
    How can i actually make this program go back to the menu when b is pressed? Im new to C++ so any tips for this script is much appreciated. Also is there any better replacement for the continuous "if/ else if"?Thanks

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Thanks! i got it to work but now i have another "problem"

    The loop works but is there a command that i can use to clear the command prompt? When the user finishes using it and wants to return to the menu, there's the history of the stuff that the user inputted on top.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    There is no standard way of doing this, however you should be able to find a method that works for you: How to clear the screen - FAQ
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No menu
    By beene in forum Windows Programming
    Replies: 13
    Last Post: 10-19-2006, 11:46 AM
  2. What is a menu?
    By Dante Shamest in forum Windows Programming
    Replies: 4
    Last Post: 08-23-2004, 09:52 AM
  3. MENU IDs
    By Granger9 in forum Windows Programming
    Replies: 1
    Last Post: 02-08-2003, 11:49 AM
  4. How to modify a menu into a menu Folder(contains submenus) ??
    By L.O.K. in forum Windows Programming
    Replies: 3
    Last Post: 01-09-2003, 02:26 PM