I\'m trying to make a calculator that adds, subtracts multiplies and divides 2 n
ID: 3620235 • Letter: I
Question
I'm trying to make a calculator that adds, subtracts multiplies and divides 2 numbers. Then, after the first operation, use the answer for the first operation as the first number in another operation.* If the user wants to clear any data they enter a C, and if they want to turn off the calculator they enter X.
* The calculator tells the user if they have entered an invalid operator.
All I could figure out so far is how to do a single operation...
include <iostream>
using namespace std;
include <stdlib.h>
void main ()
{ double a,b;
char Operator;
cout<<"Enter the first number :";
cin>>a;
cout<<"Enter the operator : ";
cin>> Operator;
cout<<"Enter second number : ";
cin>>b;
cout<<a<<Operator<<b<< " = ";
switch (Operator)
{
case '+' :
cout<<a+b<<endl;
break;
case '-':
cout<<a-b<<endl;
break;
case '*':
cout<<a*b<<endl;
break;
case'/':
cout<< a/b<<endl;
break;
default:
cout << "invalid" << endl;
cout << Operator <<" is an invalid operator. Please enter a correct operator: - , + , * , /" << endl;
break;
}
}
Thanks for any help in advance...
Explanation / Answer
please rate - thanks this should help I wasn't sure about what the C should clear #include <iostream>#include <stdlib.h>
using namespace std;
int main ()
{ double a,b,c;
char Operator;
cout<<"Enter the first number :";
cin>>a;
cout<<"Enter the operator : ";
cin>> Operator;
while(Operator!='X')
{
cout<<"Enter second number : ";
cin>>b;
switch (Operator)
{
case '+' :
c=a+b;
break;
case '-':
c=a-b;
break;
case '*':
c=a*b;
break;
case'/':
if(b==0)
cout<<"undefined ";
else
c= a/b;
break;
case 'C': a=0;
break;
default:
cout << "invalid" << endl;
cout << Operator <<" is an invalid operator. Please enter a correct operator: - , + , * , /" << endl;
break;
}
cout<<a<<Operator<<b<< " = "<<c<<endl;
a=c;
cout<<"Enter the operator : ";
cin>> Operator;
}
}