Hi, I've been learning basic C++ for the past few days, and at the moment, I'm trying to make a basic 2-number, plus/minus/multiply/divide calculator.
Here's what I have (it's complete).
Basically I was wondering if anyone could recommend a quicker or shorter way of typing all of this out.
Thanks
# include <iostream> // My first C++ Calculator!
# include <cmath>
using namespace std;
int main()
{
string exitoption;
double num1=0;
double num2=0;
while(exitoption != "x" || exitoption != "X")
{
string operationselection;
if(num1 == 0 && num2 == 0)
{
cout << "Welcome to basic calculator!\n";
cout << "Do you want an addition calculator, subtraction calculator,\nmultiplication calculator, or division calculator?\n";
}
while(operationselection != "add" && operationselection != "sub" && operationselection == "div" && operationselection == "mult")
{
operationselection = "abcabc";
cout << "(Enter add, sub, mult, or div)\n";
cin >> operationselection;
if(operationselection == "add")
{
cout << "Enter two Real numbers." << endl;
cin >> num1 >> num2;
cout << num1 + num2 << endl;
}
else if(operationselection == "sub")
{
cout << "Enter two Real numbers." << endl;
cin >> num1 >> num2;
cout << num1 - num2 << endl;
}
else if(operationselection == "div")
{
cout << "Enter two Real numbers." << endl;
cin >> num1 >> num2;
cout << num1 / num2 << endl;
}
else if(operationselection == "mult")
{
cout << "Enter two Real numbers." << endl;
cin >> num1 >> num2;
cout << num1 * num2 << endl;
}
else
{
cout << "Type one of the four choices." << endl;
}
}
if(num1 == 0 && num2 == 0)
{
num1 = 1;
}
exitoption="abcabcabc";
if(exitoption == "abcabcabc" && operationselection == "add" && operationselection == "sub" && operationselection == "div" && operationselection == "mult")
cout << "Enter 'x' to exit, or 'c' to retry Basic calculator!\n";
cin >> exitoption;
if(exitoption == "x" || exitoption == "X")
{
cout << "You are have chosen to exit.\n";
return 0;
}
while(exitoption != "x" && exitoption != "X" && exitoption != "c" && exitoption != "C")
{
cout << "Choose 'x' or 'c'.\n";
cin >> exitoption;
if(exitoption == "x" || exitoption == "X")
{
cout << "You are have chosen to exit.\n";
return 0;
}
}
}
system("PAUSE");
return 0;
}