95.2k views
0 votes
In this lab, you complete a partially written C++ program that includes a function that returns a value. The program is a simple calculator that prompts the user for two numbers and an operator ( +, -, *, or / ). The two numbers and the operator are passed to the function where the appropriate arithmetic operation is performed. The result is then returned to the main()function where the arithmetic operation and result are displayed. For example, if the user enters 3, 4, and *, the following is displayed: 3 * 4 = 12

The source code file provided for this lab includes the necessary variable declarations and input and output statements. Comments are included in the file to help you write the remainder of the program.

Instructions

Write the C++ statements as indicated by the comments.
Execute the program by clicking the Run button at the bottom of the screen.

DRAFT:

// Calculator.cpp - This program performs arithmetic, ( +. -, *. / ) on two numbers.
// Input: Interactive
// Output: Result of arithmetic operation

#include
#include

using namespace std;

// Write performOperation() function declaration here

int performOperation(double numberOne, double numberTwo, string op);
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

result = performOperation(numberOne,numberTwo,operation);
cout << numberOne;
cout << " " << operation << " ";
cout << numberTwo;
cout << " = ";
cout << result << endl;
return 0;
} // End of main() function

// Write performOperation function here
int performOperation(double numberOne, double numberTwo,std::string(op)){
double result;
if (op == "+")
result = numberOne + numberTwo;
else if (op == "-")
result = numberOne - numberTwo;
else if (op == "*")
result = numberOne*numberTwo;
else if (op == "/")
result = numberOne/numberTwo;
else
cout<<"Wrong input. Please try again.";
return result;
}

It's already working but it says that there's something wrong with the declared performOperation

User Unional
by
6.2k points

2 Answers

4 votes

The error message "Wrong input. Please try again." is displayed when the user enters an invalid operator.

C++

int performOperation(double numberOne, double numberTwo, std::string operation) {

double result;

if (operation == "+") {

result = numberOne + numberTwo;

} else

if (operation == "-") {

result = numberOne - numberTwo;

} else

if (operation == "*") {

result = numberOne * numberTwo;

} else

if (operation == "/") {

result = numberOne / numberTwo;

} else { // Handle invalid inputs

cout << "Invalid input. Please enter a valid operator (+, -, *, /): ";

cin >> operation;

return performOperation(numberOne, numberTwo, operation); // Recursive call to re-evaluate with corrected input

}

return result;

}

So, in the above modified code, if an invalid operator is entered, the program prompts the user to enter a valid operator. The performOperation() function is called again with the corrected input.

So, This ensures that the user can enter a valid operator until the arithmetic operation can be performed correctly.

1 vote

Answer:

In the given program the function work properly and the code for remainder section calculation can be given as:

Code:

This code is written inside the function in conditional statement block

else if (op == "%") //check condition

result = numberOne % numberTwo; //holds value in result variable.

Explanation:

The description of the code can be given as:

  • In the given program a function "performOperation" is defined that accepts three variable in which two values is number and one is sign value inside a function in the conditional statement section we add code for calculating remainder that can be defined above. This function will calculate a value and return the value.
  • In the main method, we declare a variable that is "numberOne, numberTwo, and operation" these three variables are used to take input from the user and passed into the function. To hold function return value we define a variable that is the result that holds function value and prints its value.
User NicholasByDesign
by
6.8k points