Calculator code with void

I am having trouble figuring out why my code is not running correctly. It builds and runs, but the output is not what I need. the output is suppose to read "Enter two numbers and a simple math operator (+,-,*,/,%,^): " then the user enters the numbers and a sign. Then the next line is suppose output the first number, then the sign, second number, equals sign, then the answer.

Here is the code, Please help. Due tonight:

#include <iostream>
using namespace std;
void calculator(int a, int b, char c, int ans = 0);

int main()
{
int num1, num2, answer;
char choice;

cout << "Enter two numbers and a simple math operator (+,-,*,/,%,^): ";
cin >> num1 >> num2 >> choice;

calculator(num1,num2,choice,answer);
cout << num1 << " " << choice << " " << num2 << " = " << answer << endl;

return 0;
}
void calculator(int a, int b, char c, int ans)
{
if (c== '+')
{
ans = a + b;
}
else if (c == '-')
{
ans = a - b;
}
else if (c == '*')
{
ans = a * b;
}
else if (c == '/')
{
ans = a / b;
}
else if (c == '%')
{
ans = a % b;
}
}
The way you've done this you need to pass answer by reference
Oops. I realized that when I enter the numbers and the math operator, I was putting them in the wrong order. Haha, thank you for the help though!!!
Topic archived. No new replies allowed.