And my code is #include \"stdafx.h\" #include <iostream> using namespace std; vo
ID: 3725221 • Letter: A
Question
And my code is
#include "stdafx.h"
#include <iostream>
using namespace std;
void main()
{
double D1(0);
double D2(0);
double D3(0);
char Operator;
const bool T(true), F(false);
do
{
cout << " Enter the first number:";
cin >> D1;
cout << " Please enter an operator (+ - * / or type X to exit or C to restart): " << endl;
cin >> Operator;
} while (Operator == 'C');
while (!F)
{
switch (Operator)
{
case '+':
cout << "Enter second number : ";
cin >> D2;
D3 = D1 + D2;
cout << D1 << " + " << D2 << " = " << D3 << endl;
T;
break;
case '-':
cout << "Enter second number : ";
cin >> D2;
D3 = D1 - D2;
cout << D1 << " - " << D1 << " = " << D3 << endl;
T;
break;
case '*':
cout << "Enter second number : ";
cin >> D2;
D3 = D1 * D2;
cout << D1 << " * " << D2 << " = " << D3 << endl;
T;
break;
case'/':
cout << "Enter second number : ";
cin >> D2;
if (D2 == 0)
{
cout << "Undefined answer(s). Enter new divisor.." << endl;
cin >> D2;
}
else D3 = D1 / D2;
cout << D1 << " / " << D2 << " = " << D3 << endl;
T;
break;
case 'C':
case 'c':
D1 = 0;
cout << " Enter first number ";
cin >> D1;
T;
break;
case 'X':
case 'x':
F;
exit(0);
break;
default:
F;
cout << "Invalid response " << Operator << " - Please enter a valid operation - Enter X to quit or enter C to clear" << endl;
}
}
D1 = D3;
cout << "Enter new operator" << endl;
}
Return operator is not working and letter C and X is not working
Please fix my problem.. Thank you
ASSIGNMENT: Modify Lab Five such that you use functions for each math calculation (Add, Subtract, Multiply, and Divide). Pass in parameters to each function for the values to use and the functions will return the result. Use a function to read in the numbers involved. These numbers will be doubles. Also write a function that reads in the operator and returns a boolean true if the operator is valid, false if not valid. This function will have two parameters. First is a "C-type" string of characters containing the valid operators. The second is a reference parameter where the operator will be placed if the operator entered is valid.Explanation / Answer
#include "stdafx.h"
#include <iostream>
using namespace std;
//declare functions
double add(double op1, double op2);
double subtract(double op1, double op2);
double multiply(double op1, double op2);
double divide(double op1, double op2);
void main()
{
double D1(0);
double D2(0);
double D3(0);
char Operator;
const bool T(true), F(false);
do
{
cout << " Enter the first number:";
cin >> D1;
cout << " Please enter an operator (+ - * / or type X to exit or C to restart): " << endl;
cin >> Operator;
//now switch on operator
switch (Operator)
{
case '+':
//call add function
cout << "Enter second number : ";
cin >> D2;
D3 = add(D1, D2);
cout << D1 << " + " << D2 << " = " << D3;
break;
case '-':
//call subtract function
cout << "Enter second number : ";
cin >> D2;
D3 = subtract(D1, D2);
cout << D1 << " - " << D2 << " = " << D3;
break;
case '*':
//call multiply function
cout << "Enter second number : ";
cin >> D2;
D3 = multiply(D1, D2);
cout << D1 << " * " << D2 << " = " << D3;
break;
case '/':
cout << "Enter second number : ";
cin >> D2;
if (D2 == 0)
{
cout << "You can't devide by zero" << endl;
}
else
{
//call devide function
D3 = divide(D1, D2);
cout << D1 << " / " << D2 << " = " << D3;
}
break;
default:
cout << "Invalid response " << Operator << " - Please enter a valid operation - Enter X to quit or enter C to clear" << endl;
break;
case 'X':
cout << "Quit..." << endl;
return ;
case 'C':
//clear the screen
system("cls");
}
} while (1);
}
double add(double op1, double op2)
{
return op1 + op2;
}
double subtract(double op1, double op2)
{
return op1 - op2;
}
double multiply(double op1, double op2)
{
return op1 * op2;
}
double divide(double op1, double op2)
{
return op1 / op2;
}
-------------------------------------------------------------------------------------------------------------------------------------
/*output
Please enter an operator (+ - * / or type X to exit or C to restart):
+
Enter second number : 56.897
89.87 + 56.897 = 146.767
Enter the first number:67.5
Please enter an operator (+ - * / or type X to exit or C to restart):
-
Enter second number : 45.78
67.5 - 45.78 = 21.72
Enter the first number:89.67
Please enter an operator (+ - * / or type X to exit or C to restart):
*
Enter second number : 78.65
89.67 * 78.65 = 7052.55
Enter the first number:89.67
Please enter an operator (+ - * / or type X to exit or C to restart):
/
Enter second number : 0
You can't devide by zero
Enter the first number:89.76
Please enter an operator (+ - * / or type X to exit or C to restart):
/
Enter second number : 2
89.76 / 2 = 44.88
Enter the first number:78
Please enter an operator (+ - * / or type X to exit or C to restart):
C
Enter the first number:89.25
Please enter an operator (+ - * / or type X to exit or C to restart):
X
Quit...
*/