I have to make a simple four-function calculator as ans assignment from class. There's various parts I don't get.
Here's the code I have written so far
#include <iostream>
#include <cstdlib>
using namespace std;
void main ()
{
int num1, num2, sum;
char op;
p:
cout << "Enter the first number\n";
cin >> num1;
cout << "Enter an operator (either +, -, *, /, C, or X)\n";
cin >> op;
while (op != '+' && op!= '-' && op != '*' && op != '/' && op!= 'C' && op!= 'X')
{
cout << "The incorrect operator has been entered. Please enter either + - * / C X as valid operators\n";
cin >> op;
}
while (op == 'C')
{
std::system("cls");
goto p;
}
cout << "Enter the second number\n";
cin >> num2;
switch (op)
{
case 43:
sum = num1 + num2;
cout << "The result is: " << sum << endl;
break;
case 45:
sum = num1 - num2;
cout << "The result is: " << sum << endl;
break;
case 42:
sum = num1 * num2;
cout << "The result is: " << sum << endl;
break;
case 47:
while(num2 == 0)
{
cout << "The denominator cannot be 0. Re-enter the the second number\n";
cin >> num2;
}
sum = num1 / num2;
cout << "The result is: " << sum << endl;
break;
}
cout << "Enter operand\n";
cin >> op;
cout << "Enter second number\n";
cin >> num2;
do
{
switch (op)
{
case 43:
cout << "The result is: " << sum + num2 << endl;
break;
case 45:
cout << "The result is: " << sum - num2 << endl;
break;
case 42:
cout << "The result is: " << sum * num2 << endl;
break;
case 47:
while(num2 == 0)
{
cout << "The denominator cannot be 0. Re-enter the the second number\n";
cin >> num2;
}
cout << "The result is: " << sum / num2 << endl;
break;
}
break;
}
while (op == '+' || op == '-' || op == '*' || op == '/' || op == 'C' || op == 'X');
}
What I still need to do is to type in X and turn off the calculator... I've tried return, std::terminate, exit, and what not, but those just terminate the rest of the code... They don't turn off the entire calculator.
Also, the last thing I need to do is to make the result of my first operation the first value of my second operation... and make that a loop so it continuously occurs as such.. Ex: I choose 7 as number 1 and 3 as number 2. If I add 7 and 3, the sum would be 10 and now I need to use 10 as number 1 for my next operation... My code already does this but only once... I need to infinitely keep on adding the result to the next number.