Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*** USING C++ *** Topics Methods Description Write a program that will implement

ID: 3838455 • Letter: #

Question

*** USING C++ ***

Topics

Methods

Description

Write a program that will implement a simple calculator. The program will prompt the user to input an operation and two decimal numbers. It will perform the specified operation on the two decimal numbers and will display the result. The program will allow the following values for operation:

+ (means add)

- (means subtract)      

* (means multiply)

/    (means divide)

The program will do the above repeatedly so that a user may perform different operations on a different pair of numbers.

/* C++ users

The program will end when the user enters x for operation.

*/

Implementation

The program will provide two methods: main and calculate.

Method calculate

The method calculate will have four parameters and will return an int value.

Below is the sample method header with an empty body. You are to fill the body with the appropriate code.

/*

          C++ Users

*/

int calculate (char op, double n1, double n2, double & res)

{

}

The method calculate will receive the following four parameters.

·An input char parameter op for receiving the operation value. (The possible values for this parameter are: +, -, *, /)

·An input double parameter n1 for receiving first operand from caller.

·An input double parameter n2 for receiving second operand from caller.

·An output double parameter res for supplying result to the caller.

Additionally, it will return an int value using return statement. If the user provides a valid value for operation, it will return a 0. Otherwise, it will return a negative number such as -1.

Method main ( )

The method main will do the following repeatedly:

·       It will prompt the user to enter the desired operation.

·       It will prompt the user to enter the first operand.

·       It will prompt the user to enter the second operand.

·       It will call the method calculate and pass it the operation, the two operands, and a result variable.

·       It will receive the result in the variable result.

·       It will display the result.

Testing

Test Run

Enter an operation

+

Enter the first number

2.2

Enter the second number

1.1

The result is 3.3

Enter an operation

-

Enter the first number

2.2

Enter the second number

1.1

The result is 1.1

Enter an operation

@

Enter the first number

2.2

Enter the second number

1.1

The operation is invalid.

Enter an operation

*

Enter the first number

2.2

Enter the second number

1.1

The result is 2.42

Enter an operation

/

Enter the first number

2.2

Enter the second number

1.1

The result is 2

Enter an operation

x (c++ users)

Bye

Explanation / Answer

#include <iostream>

using namespace std;
int calculate (char op, double n1, double n2, double & res)
{

if(op=='+'){
res= n1+n2;
return 0;
}
else if(op=='-'){
res= n1-n2;
return 0;
}
else if(op=='*'){
res= n1* n2;
return 0;
}
else if(op=='/') {
res= n1/n2;
return 0;
}
return -1;
}
int main()
{
char op;
double n1,n2,result;
while(true) {
cout << "Enter an operation: " ;
cin >> op;
if(op=='x'){
break;
}
cout <<"Enter the first number: " ;
cin >> n1;
cout << "Enter the second number: " ;
cin >> n2;
int isValid = calculate(op,n1,n2,result);
if(isValid!=-1)
cout<<"The result is "<<result<<endl;
else
cout<<"The operation is invalid."<<endl;
}
cout<<"Bye"<<endl;
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter an operation: +                                                                                                                                                                                                                                                  

Enter the first number: 2.2                                                                                                                                                                                                                                            

Enter the second number: 1.1                                                                                                                                                                                                                                           

The result is 3.3                                                                                                                                                                                                                                                      

Enter an operation: -                                                                                                                                                                                                                                                  

Enter the first number: 2.2                                                                                                                                                                                                                                            

Enter the second number: 1.1                                                                                                                                                                                                                                           

The result is 1.1                                                                                                                                                                                                                                                      

Enter an operation: x                                                                                                                                                                                                                                                  

Bye