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()
{
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.
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:
#include <iostream>
usingnamespace std;
int calc(int first, char op, int second)
{
int retVal = 0;
if(op == '+')
{
retVal = first + second;
}
elseif(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.
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.
#include <iostream>
usingnamespace std;
int calc(float first, char op, float second)
{
int retVal = 0;
if (op == '+')
{
retVal = first + second;
}
elseif (op == '-')
{
retVal = first - second;
}
elseif (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