c++ mathematical calc if-else

# c++ mathematical calc if-else - *Fanny Yang CMPSC101 Prompt...

This preview shows pages 1–3. Sign up to view the full content.

Sheet1 Page 1 /*Fanny Yang, CMPSC101 Prompt the user to enter 2 numbers(decimal) and an operator(+, -, *, /, %) Use If/else statements to perform the appropriate calculation If the user enters an invalid operator, display an error message Format the output to 3 decimal places*/ #include<iostream> #include<iomanip> using namespace std int main() { double num1, num2, answer char oper cout<<"Enter two numbers - "<<endl cout<<"Number 1: " cin>>num1 cout<<"Number 2: " cin>>num2 cout<<endl cout<<"Enter an operator (+, -, *, /, %): " cin>>oper c cout<<fixed<<showpoint<<setprecision(3) if(oper == '+') { answer = num1 + num2 cout<<"RESULTS: " cout<<num1<<" "<<oper<<" "<<num2<<" = "<<answer<<endl } else if(oper == '-') { answer = num1 - num2 cout<<"RESULTS: " cout<<num1<<" "<<oper<<" "<<num2<<" = "<<answer<<endl } else if(oper == '*') { answer = num1 * num2 cout<<"RESULTS: " cout<<num1<<" "<<oper<<" "<<num2<<" = "<<answer<<endl } else if(oper == '/') {

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Sheet1 Page 2 if(num2 == 0) cout<<"Cannot divide by zero"<<endl
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

c++ mathematical calc if-else - *Fanny Yang CMPSC101 Prompt...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online