Have no idea why this is not compiling.. Please Help!

Hi am very new to this and am having trouble even running this program. Hope this all gets easier.

Compiler is saying its that theres else statements without ifs and that the "&" requires l value on the first if statement. Any help/advide would be greatly appreciated.

# include <iostream>
# include <string>

using namespace std;

int main()
{
int a;
int b;
int c;
string d;
string e;

cout << "Please key in your equation.";
cin >> a >> d >> b >> e >> c;

if (! cin.fail()) & (((d == "+") | (d == "-")) & (e == "=")); {
if (d == "+") & (a + b == c);
cout << "Correct";
if (d == "-") & (a - b == c);
cout << "Correct";

else cout << "Incorrect";
}
else cout << "Invalid input";
}
If you use code tags, and indent your code, it makes it much easier to understand what's going on.

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
30
31
# include <iostream>
# include <string> 

using namespace std; 

int main()
{
	int a; 
	int b;
	int c;
	string d;
	string e;

	cout << "Please key in your equation.";
	cin >> a >> d >> b >> e >> c;

	if (! cin.fail()) & (((d == "+") | (d == "-")) & (e == "=")); {
		if (d == "+") & (a + b == c){
			cout << "Correct";
		}
		if (d == "-") & (a - b == c){
			cout << "Correct";
		}
		else{
		cout << "Incorrect";
		}
	}	
	else{
	cout << "Invalid input";
	}
}


Try that.

Also you don't have to but I like using {} around every if and else statment, it makes it much easier to see what is going on I find.
In your program, if you are wanting the 'AND' symbol, it is two '&' side-by side. And the 'OR' symbol is two '|' side-by side. That should get you moving in the right direction..
Most of the time we use && || but if you are playing with bitwise operator then you may actually use & | but note the result of such operation in your conditional statement.
Oh yea and there should be a semi colon at the end of that first if statement. Sorry I had really just looked at it for two seconds.
OK ive done all those changes and think im getting somewhere. Problem was to do with the brackets on line 17. One last problem is that the compiler is having a problem with the else on line 24. Its saying there is no matching if. Think it might be to do with the curly brakcets but it looks like i have the right amount in the right places. Any other suggestions?

I will get this program to tell me whether the equation i enter is correct or not.
Have got it to compile but it is most definitely not working. lol.
try this:

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>
# include <string> 

using namespace std; 

int main()
{
	int a; 
	int b;
	int c;
	string d;
	string e;

	cout << "Please key in your equation.";
	cin >> a >> d >> b >> e >> c;

	if ((!cin.fail() && (d == "+")) || ((d == "+") && (e == "="))){
		if ((d == "+") && (a + b == c)){
			cout << "Correct";
		}else if((d == "-") && (a - b == c)){
			cout << "Correct";
		}else{
			cout << "Incorrect";
		}
	}	
	else{
		cout << "Invalid input";
	}
}


I went through and was quite careful with the brackets. Wasn't sure if you wanted that middle one to be an else if or an if though. Still you should be able to work it out from here.
Every else must have a matching if... The lack of parentheses and additional semicolons are
complicating your first example...

1
2
3
4
5
6
7
8
9
10
if (! cin.fail()) & (((d == "+") | (d == "-")) & (e == "=")); { // this condition (and other others too) needs to be enclosed in a single set of ().
if (d == "+") & (a + b == c); // this semicolon means to do nothing.
cout << "Correct"; // therefore, this is always executed
if (d == "-") & (a - b == c); // same
cout << "Correct"; // same

else cout << "Incorrect"; // this else does not match up to an if, because there is an empty statement followed by the cout that always executes
} // this matches up to the first if.
else cout << "Invalid input";
} 


Now, you should also be using logical (&&) and not bitwise and (&).

See:
http://cplusplus.com/doc/tutorial/control/
Last edited on
Topic archived. No new replies allowed.