Need some help plz.....

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
#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;
}



************************************EDITED VERSON******************************
THANKS A LOT IN ADVANCED!!! IT WILL REALLY HELP MY TRAINING!!!
Last edited on
You could try using a if statement to say:

If the operator is equal to + then do addition, something like this,

if (operator == '+') n1 += n2;
@OspreyR10A, the ='==' is for the value....
"operator" is a keyword in c++, call your variable "op" or something similar.

@line 10, don't make answer equal to anything yet. just float answer;

cases for - * / @ lines 16,19,22 reference "cout<<" make them like line 13.

all of the lines cout<<"The answer is:"<<answer<<endl can come out and be replaced by a single copy of it after the switch statement between lines 26/27
Last edited on
Remove line 10. It makes no sense
lines 13,16,19,22: use single quotation marks '' (for char) instead of "" (for strings)

You don't need to repeat cout<<"The answer is:"<<answer<<endl for each case. Just place it below the switch

[EDIT]
do not use operator as a name for a variable. It's a keyword in C++ (hence it's blue...)
Last edited on
Hi guys, i did everything, but nothing worked :/ there is still syntax error...
there is still syntax error...
I suppose it might be useful to mention what the actual error is. And also share the updated version of your code please.
this is the updated version...
You still have the wrong quotation marks: "" instead of correct ''
still not working :/
the error is in LINE 8
The error is in line 7, float answer - missing semicolon.
now the error is in line 17. Im sorry, i have only taken 2 classes of C++
line 16: another semicolon is missing
it is donE!!!! thanks loads my friends!!!!!!!
You're missing two semi colons after statement line 7 and 16.
@ashwinmo Please start a new thread with your question rather than diverting some existing thread.
Topic archived. No new replies allowed.