Complete a partially written C++ program that includes a function that return a
ID: 668242 • Letter: C
Question
Complete a partially written C++ program that includes a function that return a value. The program is a simple calculator that prompts the user of 2 number and an operation (+, -, * or, /). The two number and the operator are passed to the function where the appropriate arithmetic operation is performed. The result is return to the main function () where the arithmetic operation and result are displayed. For example 3 * 4 = 12
The source code file provided includes the necessary variable declaration and input and output statement. Comments are included in the file to help you write the reminder program.
// Arithmetic2.cpp - This program performs arithmetic, ( +. -, *. / ) on two numbers.
// Input: Interactive
// Output: Result of arithmetic operation
#include <iostream>
#include <string>
using namespace std;
// Write performOperation() function declaration here
int main()
{
double numberOne, numberTwo;
string operation;
double result;
cout << "Enter the first number: ";
cin >> numberOne;
cout << "Enter the second number: ";
cin >> numberTwo;
cout << "Enter an operator (+.-.*,/): ";
cin >> operation;
// Call performOperation method here
cout << numberOne;
cout << " " << operation << " ";
cout << numberTwo;
cout << " = ";
cout << result << endl;
return 0;
} // End of main() function
// Write performOperation function here
Explanation / Answer
Simple calculator for addition,subtraction,multiplication,division
# include <iostream>
using namespace std;
int main()
{
char o;
float num1,num2;
cout << "Enter operator either + or - or * or /: ";
cin >> o;
cout << "Enter two operands: "; //Enter two numbers
cin >> num1 >> num2;
switch(o) {
case '+':
cout << num1+num2; //for addition operation
break;
case '-':
cout << num1-num2; //for subtraction operation
break;
case '*':
cout << num1*num2; //for multipication operation
break;
case '/':
cout << num1/num2; // for division operation
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
cout << "Error! operator is not correct";
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.