C++ Answers Only You are to write a simple calculator that will perform basic ar
ID: 3883257 • Letter: C
Question
C++ Answers Only
You are to write a simple calculator that will perform basic arithmetic operations on two doubles.
1. Your calculator should handle arithmetic operation of two doubles that adhere to the following form: <double> <operator> <double>
2. Your calculator should prompt the user to inquire for an additional calculation or to quit. This process should repeat until the user agrees to quit.
3. Your program should contain the following functions (note exact spelling is required):
add()
Takes in two doubles and returns the sum of the two doubles.
subtract()
Takes in two doubles and returns the first double minus the second.
multiply()
Takes in two doubles and returns the product of the two doubles.
divide()
Takes in two doubles and returns the first double divided by the second.
getInput()
Responsible for getting the input from the user. Should prompt the users for two doubles and a char in the form <double> <char> <double>.
Explanation / Answer
//Please see the code below:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
double add(double x, double y)
{
return (x+y);
}
double subtract(double x, double y)
{
return (x-y);
}
double multiply(double x, double y)
{
return (x*y);
}
double divide(double x, double y)
{
return (x/y);
}
void getInput(double *x,char *opt, double *y)
{
cout<<"Enter two doubles and a char in the form <double> <char> <double>."<<endl;
cin>>*x;
cin>>*opt;
cin>>*y;
}
int main()
{
double x,y;
char opt;
int exit=1;
while(1)
{
getInput(&x,&opt,&y);
switch(opt)
{
case '+':
cout<<"Add of two doubles is "<<add(x,y)<<endl;
break;
case '-':
cout<<"Subtract of two doubles is "<<subtract(x,y)<<endl;
break;
case '*':
cout<<"Multiply of two doubles is "<<multiply(x,y)<<endl;
break;
case '/':
cout<<"Divide of two doubles is "<<divide(x,y)<<endl;
break;
default:
cout << "Error! operator is not correct";
break;
}
cout<<"press 0 to exit and 1 to continue"<<endl;
cin>>exit;
if(exit==0)
break;
}
return 0;
}
OUTPUT:
Enter two doubles and a char in the form <double> <char> <double>.
2.2 + 2.1
Add of two doubles is 4.3
press 0 to exit and 1 to continue
1
Enter two doubles and a char in the form <double> <char> <double>.
2.2 - 1.1
Subtract of two doubles is 1.1
press 0 to exit and 1 to continue
1
Enter two doubles and a char in the form <double> <char> <double>.
1.2 * 1.2
Multiply of two doubles is 1.44
press 0 to exit and 1 to continue
1
Enter two doubles and a char in the form <double> <char> <double>.
4.2 / 1.2
Divide of two doubles is 3.5
press 0 to exit and 1 to continue
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.