Jul 4, 2014 at 9:41am UTC
Hi all,
i am creating a program for simple calculation (+,-,*,/) and i need the user to give the 1st number, operator, 2nd number. This is what i did, is this right, whats my mistake?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
float n1;
float n2;
char op;
float answer
cin>>n1;
cin>>op;
cin>>n2;
switch (op)
{
case "+" : answer=(n1+n2);
cout<<"The answer is:" <<answer<<endl
break ;
case "-" : answer=(n1-n2);
cout<<"The answer is:" <<answer<<endl
break ;
case "x" : answer=(n1*n2);
cout<<"The answer is:" <<answer<<endl
break ;
case "/" : answer=(n1/n2);
cout<<"The answer is:" <<answer<<endl
break ;
default : cout<<"Make sure you are using, '+' for addition, '-' for subtraction, '*' for multiplication and '/' for division!" ;
}
return 0;
}
THANKS A LOT IN ADVANCE, THIS IS GOING TO HELP ME A LOT!!!
Last edited on Jul 4, 2014 at 11:08am UTC
Jul 4, 2014 at 10:37am UTC
You called your char op but in the switch statement, you have written switch (operator) instead of op.
Jul 4, 2014 at 11:18am UTC
In your case statements, you are using double-quotes. These are string literals, so their value is a memory address, i.e. a pointer to char.
For single char variables, you should use single quotes.