Arithmetic Operations

I am having issues figuring out where to start in an assignment I was given. I'm currently taking Programming I for C++ and my professor can be very hard to understand at times so I typically self teach. The instructions for the assignment are below. If someone wouldn't mind helping me get to a good starting point or explaining the different points to me so I can better understand, that would be awesome. My class uses Visual Studio. Thank you!



Write a C++ program with a function that will perform arithmetic operations (addition, subtraction, multiplication, and division) and return the result to the caller. The function should be defined with three constant parameters.

The first two will be reference parameters of double type. These will serve as the operands
The third reference parameter (a character type) will serve as the operator
The return type should be double

Read operands and operators in the main function and call the function to do the operation. The result retuned must be displayed with appropriate message. The program should terminate when 0s are read for the operands.

For example, if the user enters

10.5 20 +

The main program should read and call the function with these three arguments. The function must return the result as 30.5. The main function must produce the following output

10.5 + 20 is 30.5
Last edited on
Start it like any other program. Start with your main function. First, you are going to need to get the user input. So set up the 3 variables (2 doubles, 1 char) and set up the statements to get the users input. After this, you can start setting up the function. As it says you will be passing the variables as constants and by reference. Inside the function, you'll need a simple if/if else statements to test the char you passed which will tell you whether you will be doing addition, subtraction, multiplication, and division. From there, you do the math and return the result. Back in main, you will output the result.

If there is a specific spot you are stuck at please be more specific.
Last edited on


#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
double num = 0;
double num2 = 0;
char op = '+';
cout << "Enter two numbers (largest to smallest) and enter +, -, *, or /: ";
cin >> num >> num2 >> op;
cout << "First number: " << num << endl;
cout << "Second number: " << num2 << endl;
cout << "Operation: " << op << endl;

if (op = '+')
num + num2;
cout << "The Sum is: " << op << endl;

if (op = '-')
num - num2;
cout << "The Difference is: " << op << endl;

if (op = '*')
num * num2;
cout << "The Quotient is: " << op << endl;

if (op = '/')
num / num2;
cout << "The Quotient is: " << op << endl;







return 0;
}


So what I'm stuck on now is how I would write the If statements. It doesn't do the operation that I state and only outputs the operation symbol.
= is assignment.
Use comparison: ==
Tried changing to == and no change in the output.
Okay so I realized what Joe had said and misunderstood. I have put together my if statement now how would I code in the equations after to get the desired answers?
Hint;
if (op == '+') std::cout << (num + num2) << '\n';
Last edited on
closed account (48T7M4Gy)
Don't forget that a function which takes the operator and operand as parameters, and returns a double ( the answer) is required to perform the calculations.

Also a switch control inside the function is a good way to improve on the cascade of if statements.
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
Topic archived. No new replies allowed.