Functions

I NEED HELP WITH THIS PROBLEM!!!

Create a simple calculator using Function. The program should take an arithmetic operator +, -, *, / and two operands from the user. Then, it performs the calculation on the two operands depending upon the operator entered by the user.

1. Create a main function in a main.cpp file. The main function should look as follows int main()
{return 0;}
2. Ask the user to enter two numbers (float) and an operator (Char)
3. Create a Function for each operator and pass the numbers to the it.
4. Do the require operation inside the function, return the result to the main.
5. Display the result in the main function.

Thank you for the help but I am still getting the wrong answer although the code is much more improved!

#include <iostream>
using namespace std;

int add (float first, char op,float second)
{
float a;
a = first + second;
return (a);
}
int subtract(float first, float second)
{
float b;
b = first - second;
return b;
}
int multiply(float first, float second)
{
float c;
c = first * second;
return c;
}
int divide(float first, float second)
{
float d;
d = first / second;
return d;
}
int main()
{

float first, second;
char op;
cout << "Enter 2 operants:" << endl;
cin >> first >> second;

cout << "Enter an operator: ";
cin >> op;

cout <<add<<subtract<<multiply<<divide;
return 0;
}
Last edited on
Show us what you've got so far, or where are you currently stuck?

Note: the arithmetic operator can be retrieved as a char.

Last edited on
the instructions are mutex... main can't look like that, if you want the program to do anything at all. 5) contradicts 1). :P Just picking on your prof for being careless.

This isnt a homework site .. we love to teach and help, but you need to do the work.
Last edited on
@jonnin,

I understood instruction 1 to mean that main() should have a return type of int (as opposed to the non-standard void return type) and it should explicitly return '0' at the end.

So, the template for the main() function would be:

1
2
3
4
5
6
int main()
{
    // PUT YOUR CODE HERE

    return 0;
}
I know. It stuck me funny, is all.
Let me write some example of the calc function that will add/subtract two numbers, I think you should be able to go from there...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int calc(int first, char op, int second)
{
    int retVal = 0;
    if(op == '+')
    {
        retVal = first + second;
    }
    else if(op == '-')
    {
        retVal = first - second;
    }
    return retVal;
}

int main()
{
    cout << calc(3, '+', 7) << endl;
    return 0;
}


Note: I did not follow the instructions per word; this is an example of how it could be done with ints, not floats. It does not ask for user input.
I wanted to show the if/else if, and how to call a function as output to cout. The instructions do say that the outputs should be handled all in main.
Last edited on
If I read instruction 3 correctly, and it is possible I didn't, the professor wants 4 functions. One for each operator. Only the numbers are to be passed, not the operator. The entered operator simply determines which function to call.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

int calc(float first, char op, float second)
{
    int retVal = 0;
    
    if (op == '+')
    {
        retVal = first + second;
    }
    else if (op == '-')
    {
        retVal = first - second;
    }
    else if (op == '*')
    {
        retVal = first * second;
    }
    else // <--
    {
        retVal = first / second;
    }
    
    return retVal;
}

int main()
{
    cout << calc(2, '+', 7) << endl;
    return 0;
}


PS A switch statement is another way but your problem was the op == '/' .
You'll probably score extra points if you allow in the division to check for division by zero.
PPS Your function returns an int which is not a good move for floating point division
Last edited on
Topic archived. No new replies allowed.