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