Hello all.(adsbygoogle = window.adsbygoogle || []).push({});

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)Code (Text):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;

}

**Physics Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Simple Calculator Problem (C++)

Loading...

Similar Threads - Simple Calculator Problem | Date |
---|---|

Some simple problem with my GUI? | Nov 8, 2017 |

C/++/# Creating a simple boolean logic calculator in C | Sep 28, 2016 |

C++ Super Simple Download Calculator (if/else stuff) | Jan 18, 2014 |

Can someone write me a small wxPython program - Simple calculation | Apr 10, 2011 |

Simple Fortran Reading Values and Calculations | Mar 10, 2010 |

**Physics Forums - The Fusion of Science and Community**