Calculator Exercise



#include <iostream>
using namespace std;
//create a calculator
int main ()
{
double a, b;
cout << "Welcome to Calculator!" << endl << endl;
beginning: // from goto at the bottom
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
cout << endl; //a gap
cout << "How do you want to operate these two numbers? (Only insert a symbol)" << endl;
cout << "+ (Addition)" << endl;
cout << "- (Subtraction)" << endl;
cout << "* (Multiplication)" << endl;
cout << "/ (Division)" << endl;
cout << "^ (Power)" << endl;
cout << "% (Modulo)" << endl << endl;
//make decision for calculating those 2 numbers
char decision1;
cout<< "Enter Here: ";
cin >> decision1;
system("cls");
switch(decision1)
{
case '+':
cout << a << "+" << b << "=" << (a+b) << endl;
break;
case '-':
cout << a << "-" << b << "=" << (a-b) << endl;
break;
case '*':
cout << a << "*" << b << "=" << (a*b) << endl;
break;
case '/':
if(b) //same as b != 0
cout << a << "/" << b << "=" << (a/b) << endl;
else
cout << "You can't use zero to divide";
break;
case '^':
int i;
float raised;
raised = 1;
for(i=1;i<=b;i++)
{
raised *= a;
}
cout << a << "^" << b << "=" << raised << endl;
break;
case '%':
int c, d; //for substituting "a" and "b", since they are float type, we need to use integer
cout << "Re-enter first number (only integer): " << endl;
cin >> c;
cout << "Re-enter second number (only integer): " << endl;
cin >> d;
cout << c << "%" << d << "=" << (c % d) << endl;
break;
default:
cout << "You typed the wrong symbols.";
}
//do you want to continue using the calculator?
char decision2;
cout << "Do you want to continue using the calculator? (Y|N)?: ";
cin >> decision2;
cout << endl; //a gap
if(decision2 == 'y' || decision2 == 'Y' )
goto beginning;
}
#include <iostream>
using namespace std;
//create a calculator
int main ()
{
double a, b;
cout << "Welcome to Calculator!" << endl << endl;
beginning: // from goto at the bottom
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
cout << endl; //a gap
cout << "How do you want to operate these two numbers? (Only insert a symbol)" << endl;
cout << "+ (Addition)" << endl;
cout << "- (Subtraction)" << endl;
cout << "* (Multiplication)" << endl;
cout << "/ (Division)" << endl;
cout << "^ (Power)" << endl;
cout << "% (Modulo)" << endl << endl;
//make decision for calculating those 2 numbers
char decision1;
cout<< "Enter Here: ";
cin >> decision1;
system("cls");
switch(decision1)
{
case '+':
cout << a << "+" << b << "=" << (a+b) << endl;
break;
case '-':
cout << a << "-" << b << "=" << (a-b) << endl;
break;
case '*':
cout << a << "*" << b << "=" << (a*b) << endl;
break;
case '/':
if(b) //same as b != 0
cout << a << "/" << b << "=" << (a/b) << endl;
else
cout << "You can't use zero to divide";
break;
case '^':
int i;
float raised;
raised = 1;
for(i=1;i<=b;i++)
{
raised *= a;
}
cout << a << "^" << b << "=" << raised << endl;
break;
case '%':
int c, d; //for substituting "a" and "b", since they are float type, we need to use integer
cout << "Re-enter first number (only integer): " << endl;
cin >> c;
cout << "Re-enter second number (only integer): " << endl;
cin >> d;
cout << c << "%" << d << "=" << (c % d) << endl;
break;
default:
cout << "You typed the wrong symbols.";
}
//do you want to continue using the calculator?
char decision2;
cout << "Do you want to continue using the calculator? (Y|N)?: ";
cin >> decision2;
cout << endl; //a gap
if(decision2 == 'y' || decision2 == 'Y' )
goto beginning;
}