else without a previous if error

Jul 27, 2018 at 2:38pm
So, no matter what i do, i can't seem to be able to fix this error, could someone please help me?

[code]
#include <iostream>
using namespace std;
string input1;
string input2;
int main()
{
cout<< "Hello World, My Name is Jeffery."<<endl;
cout<< "What is Your Name?"<<endl;
cin>> input1;
cout<< "Nice to Meet You, "<< input1 << "!"<<endl;
cout<< "Would You Like To Play A Game "<< input1 <<"?"<<endl;
cin >> input2;
if (input2 == "Yes" || "yes");
{
cout << "Alright, let's play a story building game"<< endl;
}
else (input2 == "no" || "No");
{
exit(0);
}
}


Thank You so much for all the help, I really appreciate it!
Last edited on Aug 1, 2018 at 3:01pm
Jul 27, 2018 at 2:42pm
1. Don't put semi-colons after if or else statements.
2. else statements have no condition. This follows logically, because it's like saying "otherwise... do this".
3. Each logical comparison must stand on its own.

1
2
3
4
if (input2 == "Yes" || input == "yes")
{

}

1
2
3
4
else
{

}


Also, the exit(0) is unnecessary, which extends to saying your entire else statement is pointless.
Last edited on Jul 27, 2018 at 2:44pm
Jul 27, 2018 at 2:42pm

else (input2 == "no" || "No");


Should be...
1
2
3
else{
exit(0);
}


if (input2 == "Yes" || "yes");

should be...
1
2
3
if(input2 == "Yes" || input2 == "yes") {
cout << "Alright, let's play a story building game"<< endl;
}
Last edited on Jul 27, 2018 at 2:46pm
Jul 27, 2018 at 2:45pm
else does not take a condition. It says "if no other condition is met, use this branch". What you're looking for is else if. IE

1
2
3
4
5
6
7
if(myvar == 1) {
  // ...
} else if(myvar > 3) {
  // ...
} else {
  // ...
}


As a side note, input2 == "no" || "No" doesn't do what you think it does. You need to write: input2 == "no" || input2 == "No". You could also simply make that something like: some_tolower_function(input2) == "no"
Last edited on Jul 27, 2018 at 2:51pm
Jul 27, 2018 at 3:39pm
FYI:
c++ does not care about whitespace in the code.
c++ does not have an elif or elseif type statement.

put those 2 together, and understand that a lot of code has

if (blah)
else if (other)
else

looking blocks, but the else and the if are really 2 statements on a single line.
its really just a condensed version of chained condition statements.

exit zero is heavily frowned upon, or used to be. It does not clean up the program well. You should use return(0) in main instead, or terminate() if you really, really need to self-destruct the program somewhere. Its not for ending the program normally, its for critical failures ... if you need to end the program, provide a normal path to do that (hitting a return statement in main is the normal path). You may have multiple return statements in main (and any other function as well).
Last edited on Jul 27, 2018 at 3:39pm
Jul 27, 2018 at 6:16pm
Hello! Else is not followed by conditions! Its not else (...condition...) do smth, its only else do smth! Secondly, dont put semicollons "{}" after each if or else statement if you only have one action. Add semicollons when you want your if statement to do more than one action. Also, instead of exit(0), add return 0. Hope I helped! Good luck.
Aug 1, 2018 at 3:05pm
Thanks everyone for all the awesome help, it really helps me out. Thank You!
Topic archived. No new replies allowed.