Need some help plz.....

Jul 4, 2014 at 9:43am
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 Jul 4, 2014 at 11:09am
Jul 4, 2014 at 9:56am
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;
Jul 4, 2014 at 10:01am
@OspreyR10A, the ='==' is for the value....
Jul 4, 2014 at 10:09am
"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 Jul 4, 2014 at 10:11am
Jul 4, 2014 at 10:11am
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 Jul 4, 2014 at 10:12am
Jul 4, 2014 at 10:31am
Hi guys, i did everything, but nothing worked :/ there is still syntax error...
Jul 4, 2014 at 10:36am
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.
Jul 4, 2014 at 11:03am
this is the updated version...
Jul 4, 2014 at 11:07am
You still have the wrong quotation marks: "" instead of correct ''
Jul 4, 2014 at 11:10am
still not working :/
Jul 4, 2014 at 11:10am
the error is in LINE 8
Jul 4, 2014 at 11:12am
The error is in line 7, float answer - missing semicolon.
Jul 4, 2014 at 11:17am
now the error is in line 17. Im sorry, i have only taken 2 classes of C++
Jul 4, 2014 at 11:18am
line 16: another semicolon is missing
Jul 4, 2014 at 11:18am
it is donE!!!! thanks loads my friends!!!!!!!
Jul 4, 2014 at 11:18am
You're missing two semi colons after statement line 7 and 16.
Jul 4, 2014 at 12:57pm
@ashwinmo Please start a new thread with your question rather than diverting some existing thread.
Topic archived. No new replies allowed.