ok, here is where you are going wrong.
First you are calling each function in sequential order (so the add funtion is done first, then the sub function, etc....) , so if the user's first input is 2 3 -, this is being handled by your add function, which is only checking for oper == '+'. Since oper isn't equal to the +, it is just outputting ans (which is a float), here is where your are getting your undefined behavior, since your are declaring: float ans, but you are not assigning it an initial value, so it is pulling some number out of the memory space where float ans resides. If you go back into your program, and set each float ans = 0 when you initialize them, and do a test where oper fails you will get a zero instead of the 1.38168e-41 (or some number similar). Not only that, but if you enter 5 + 5 at the command prompt, you are assigning float x == 5, float y == +, and char oper == 5. The way your have your inputs set up, you need to do the following: 5 5 + or 5 5 - or 5 5 * or 5 5 / (I overlooked this the first time I posted this information)
Not to sound picky, but it is a good habit to declare each variable you use on it's own line: (eg)
not only that, but if you want to make sure you don't run into undefined behavior, it is always a good thing to initialize your variables with a default value:
float x = 0;
float y = 0;
float ans = 0;
the other thing you need to do is include an endl after your cout << ans.
cout << ans << endl;
which will output the next line in your code on the next line in the terminal.
I am including my example of your program, which takes the oper input from the user input, and passes it to the appropriate function depending on the value. The other minor thing I did was to check to make sure that the denominator isn't zero in the division function and setting it to 1 if it is.
using namespace std;
void add( float, float );
void sub( float, float );
void mult( float, float );
void divd( float, float );
cout << "Input two numbers and an operator ";
cin >> a >> b >> oper;
switch( oper )
add( a, b );
sub( a, b );
mult( a, b );
divd( a, b );
return( 0 );
void add( float x, float y )
cout << "Answer: " << x + y << endl;
void sub( float x, float y )
cout << "Answer: " << x - y << endl;
void mult( float x, float y )
cout << "Answer: " << x * y << endl;
void divd( float x, float y )
if( y == 0 )
cout << "Can't divide by zero, setting denominator to 1" << endl;
y = 0;
cout << "Answer: " << x / y << endl;
The other thing that you'll notice is that I declare my functions with a function header call before main, and do all my functions them self afterwards. If you are just starting out, now is a good time to learn how to make your code look clean, it will help you out drastically in the long run.
Another good tip, if you use the terminal to write your programs, is to learn vim, it is an awesome editor, which will run circles around most others, after learning how to use it. The learning curve is a little steeper than using something like pico, but if/when you get into bigger more complex programs it will save you time in the long run. I'm including a link from my instructor's website on some of the commands for vim/vi (vim is an improved version of vi, and is now included with 10.3)
If you have any questions about what I've written or about vim, please feel free to contact me. (e-mail is in profile)
Good luck and happy coding