### How to take arithmetic operations as input..?

Hi all I'm new to the forums, and to C++. I decided to try and teach myself since outside of a class in MATLAB (where we basically only worked on one project) and a class in Visual Basic in college, I didn't get much programming experience. This is unfortunate since the vast majority of job openings in my field (Meteorology) seem to want you to have at least some ability in some form of programming language. The same holds true for the graduate programs I've researched. Thus, it seems like I'd better get to work learning one and C++ (or so I've read) is a great 'first' one to learn.

I decided on the book 'Jumping into C++' and I've been thrilled with it thus far. Today I've been doing the Chapter 4 review questions and ran into one I didn't quite get.

The question reads "Write a small calculator that takes as input one of the four arithmetic operations, the two
arguments to those operations, and then prints out the result."

I'm confused as to what it means by 'takes as input one of the four arithmetic operations'. I Googled around and found some examples of what I think the book wants using 'switch' and 'case' but I haven't learned those yet.

Chapter 4 was about if, else, else if statements, relational operators, and Boolean operators (not, and, or).. previous chapters dealt with variables (char, int, double), strings and other extremely basic things like syntax, remembering semicolons etc.

Here's what I tried to do..

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include using namespace std; int main () { char input; int num1, num2; cout << "Please enter the first number: \n"; cin >> num1; cout << "Please enter the second number: \n"; cin >> num2; cout << "Now, please choose an arithmetic operator (Arithmetic operators are /, *, + and -): \n"; cin >> input; if ( input == "/" ) { cout << num1 << "/" << num2 << "=" << num1 / num2 << "\n"; } else if ( input == "*" ) { cout << num1 << "*" << num2 << "=" << num1 * num2 << "\n"; } else if ( input == "+" ) { cout << num1 << "+" << num2 << "=" << num1 + num2 << "\n"; } else if ( input == "-" ) { cout << num1 << "-" << num2 << "=" << num1 - num2 << "\n"; } else { cout << "Sorry, you entered an invalid operator.\n"; } }``````

.. Which doesn't work at all due to me trying to compare input to a string literal. I didn't think it would work but I was at a loss on how to proceed otherwise.

I'm sure I'm missing something that will make me go "D'OH" but I can't figure out what it is.

Sorry for the super long post, thank you very much for reading. Moderators please yell at me if I did anything stupid.. being my first post and all.. thank you x)
Last edited on
If you want to simply compare a character, use ' instead of ":

`if(input == '+')`

Also, use code tags next time, it'll preserve indentation and stuff.
Is that the output option in the format buttons?

And thank you very much, I knew it was simple... little did I know how simple. I'll give it a shot!
Nope, the code one (duh). That's enough embarrassment for one day I think. Thanks again for the answer, everything works great now and I assume that's what the question was looking for.
Topic archived. No new replies allowed.