- #1

- 773

- 0

## Main Question or Discussion Point

Hello all.

I'm teaching myself C++ (my first programming language), and as an exploration I decided to see if I could make a simple four-function calculator. The thing is I'm having trouble. I managed to successfully write a program that would input two integers and return all four functions of the two (their sum, difference, quotient and product) but I couldn't get it to just give me one at a time (a problem with my if clauses I think).

So I started from scratch and wrote this:

Now I get a return value of 0, regardless of the input integers and the chosen function. I'm new to C++, so could anyone tell me what I'm doing wrong?

Thank you.

(I didn't copy and paste the trivial stuff like the function definitions but I'm not worried that's the problem)

I'm teaching myself C++ (my first programming language), and as an exploration I decided to see if I could make a simple four-function calculator. The thing is I'm having trouble. I managed to successfully write a program that would input two integers and return all four functions of the two (their sum, difference, quotient and product) but I couldn't get it to just give me one at a time (a problem with my if clauses I think).

So I started from scratch and wrote this:

Now I get a return value of 0, regardless of the input integers and the chosen function. I'm new to C++, so could anyone tell me what I'm doing wrong?

Thank you.

Code:

```
int chooseOperator(int OP, int num1, int num2)
{
if (OP == '+')
return add(num1, num2);
if (OP == '-')
return subtract(num1, num2);
if (OP == '*')
return multiply(num1, num2);
if (OP == '/')
return divide(num1, num2);
else
return 0;
}
int main()
{
using namespace std;
cout << "Thank you, will you please enter your first integer:" << endl;
int num1;
cin >> num1;
cout << "Thank you, will you please enter your second integer:" << endl;
int num2;
cin >> num2;
cout << "Will you please choose an operator:" << endl;
int O;
cin >> O;
cout << chooseOperator(O, num1, num2);
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}
```