'Changed if - if functions to if - else, and I get "else without previous if" [closed]
Recently I have been making a text-based game to imitate a game from the 90's for my friend, and it went all well, but the Yes/No didn't work because I had an if statement then another if statement following first, so then I change it to an if statement with an else statement following the if statement, but get error's like "else without previous if" and "label referenced but not defined" which is really weird because a few lines below the reference in the defined label... I really need help with this, because I tried googling it but comes up with useless Microsoft threads that don't relate whatsoever.
My Code:
#include <iostream>
int main() //main function
{
std::cout << "NOTE: when you see [Y/N], it is a choice option, please capitalize letter of choice!!\n";
lab1:
char name = NULL;
int age = NULL;
char ans1 = NULL;
char strong = NULL;
char ans2 = NULL;
char ans3 = NULL;
char smart = NULL;
char ans4 = NULL;
char ans5 = NULL;
std::cout << "Well hello! What is your name?\n";
std::cin >> name;
std::cout << "And your age is?\n";
std::cin >> age;
std::cout << "Ok so your name is " << name << " and you are " << age << "?\n";
std::cout << "Is that correct? [Y/N]\n";
std::cin >> ans1;
if (ans1 == 'Y');
{
std::cout << "Well then, lets get started with your main ability!\n";
goto lab2;
}
else
{
std::cout << "Oh sorry, my bad lets enter it again shall we?\n";
goto lab1;
}
lab2:
lab4:
lab6:
std::cout << "Strong? [Y/N]\n";
std::cin >> strong;
if (strong == 'Y');
{
std::cout << "Are you sure? Only ONE main ability can be chosen! [Y/N]\n";
std::cin >> ans2;
if (ans2 == 'Y')
{
goto lab3;
}
else
{
goto lab4;
}
}
else
{
std::cout << "Are you sure? Move to next ability? [Y/N]\n";
std::cin >> ans3;
if (ans3 == 'Y');
{
goto lab5;
}
else
{
goto lab6;
}
}
lab5:
lab8:
lab10:
std::cout << "Smart? [Y/N]\n";
std::cin >> smart;
if (smart == 'Y')
{
std::cout << "Are you sure? Only ONE main ability can be chosen! [Y/N]\n";
std::cin >> ans4;
if (ans4 == 'Y')
{
goto lab7;
}
else
{
goto lab8;
}
}
else
{
std::cout << "Are you sure? Move to next ability? [Y/N]\n";
std::cin >> ans5;
if (ans5 == 'Y')
{
goto lab9;
}
else
{
goto lab10;
}
}
lab9:
lab3:
lab7:
std::cout << "Done!";
}
Solution 1:[1]
I know this doesnt answer your question but I am going to give advice.
Code like this with that many labels is very fragile (meaning its hard to change as your project grows). I have worked on large c++ projects (100,000 lines +) and every goto and label had to be individually justified. Many probably had none. You should develop a dislike of typing the characters 'g' 'o' 't' 'o'.
So how do you do what you code is trying to do. The common idiom is
std::string ans;
while(true)
{
std::cout >> "Are you sure";
std::cin >> ans;
if(ans == "Y" || ans == "N")
break;
}
if (ans == "Y")
{
//do the yes thing
}
else
{
// do the no thing
}
This will keep looping , asking the user a Yes No question until they answer Y or N
Taking your example of Smart
bool isSmart = false;
while(true)
{
std::cout << "Smart? [Y/N]\n";
std::cin >> smart;
if (smart == 'Y')
{
std::cout << "Are you sure? Only ONE main ability can be chosen! ";
std::cin >> ans4;
if (ans4 == 'Y')
{
isSmart = true;
break;
}
}
}
EDIT, answer the original question
You have
if (ans3 == 'Y');
{
goto lab5;
}
else
{
goto lab6;
}
this is invalid syntax
you need
if (ans3 == 'Y') // <=== no ;
{
goto lab5;
}
else
{
goto lab6;
}
You have this same error in multiple places
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
