Write a C++ program that mimics a calculator. The program should prompt the user
ID: 3620422 • Letter: W
Question
Write a C++ program that mimics a calculator. The program should prompt the user and take as input a number, an operator such as + , - , / , * followed by a second number. It then should output the first number, the operator, the second number and the results. Check to make sure division by zero is not performed by your program. Some sample /input output are as follows:Input Output
1 + 5 1 + 5 = 6
1 - 9 1 – 9 = -8
10 / 3 3.33
1 / 0 Error – division by zero
2 * 100 2 * 100 = 200
2 % 20 Try again
2 Q 1 Exit program
Your program must make use of if-else, while loop, and switch control structures.
Explanation / Answer
please rate - thanks #include <iostream>#include <string.h>
using namespace std;
int main()
{
char oper;
int var1 = 0, var2 = 0;
while(true)
{cout<<"enter variable1 operation variable 2 (ex 1 + 2) ";
cin>>var1;
cin>>oper;
cin>>var2;
cout<<var1<<oper<<var2<<" = ";
switch(oper)
{case '+':cout<<var1+var2;
break;
case '-':cout<<var1-var2;
break;
case '*':cout<<var1*var2;
break;
case '%':if(var2>var1)
cout<<"Try again";
else
cout<<var1%var2;
break;
case '/':if(var2==0)
cout<<"Error-division by zero";
else
cout<<(double)var1/var2;
break;
case 'q':
case 'Q': return 0;
default: cout<<"Invalid operator";
}
cout<<endl;
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.