Text-adventure.cpp - error message 'else' without a previous 'if'

I’m working on text-adventure.cpp (it is not complete) and have an error message for lines 46, 66 and 70 saying ‘else’ without a previous ‘if’. I just cannot see where I’m going wrong.

I’m trying to use nested if statements and wonder if this is causing confusion.

‘’’
`#include
int main() {

char choice1,choice2, choice3;

char A,B,C;

std::cout << " You are one of the most villainous and grumpiest criminals of all time.\n ";
std::cout << " You employ a small army of hapless yellow Minions dressed in blue outfits.\n ";
std::cout << " You are planning to steal the Moon, the biggest heist in the history of the world!\n ";
std::cout << " One day you encounter three orphans who see you as a potential Dad.\n ";
std::cout << " You have met your greatest challenge yet.\n ";
std::cout << " What do you do?\n ";

std::cout << " Do you?\n";

std:: cout << " A. Ignore the three orphans? OR \n";
std::cout << “B. Decide to use them in your cunning plan to steal the Moon? \n”;
std::cout << " Enter your answer, A or B:";
std::cin >> choice1;

if (choice1 == A){
std::cout << “You successfully steal the Moon!\n”;
std::cout << “Do you?\n”;
std::cout << “A. Live happily ever after knowing you’ve completed the biggest heist in the history of the world?\n”;
std::cout << “B. Feel regret and misery at ignoring the three orphans?\n”;
std::cout << “C. Go back to the orphanage and adopt the three girls anyway?\n”;
std::cout << “Enter your answer, A,B or C:”;
std::cin >> choice2;
}
else if (choice2 == A){
std::cout << “Congratulations on capturing the Moon!\n”;
std::cout << “Do you?\n”;
std::cout << “A. Live a life of endless parties spending all your money until you’re broke\n”;
std::cout << “B. Invest your time into self promotion so everyone knows you’re the most successful villain ever\n”;
std::cout << “Enter your answer, A or B:”;
std::cin >> choice3;
}
if (choice3 == A){
std::cout << “You join a villains self-help group and become a goat herder\n”;
}
else if(choice3 == B);{
std::cout << “You reach the ultimate upper echelons of infamy and spend the rest of your days handing out signed photos.\n”;
}
else
std::cout << “Incorrect”;

/**************************************/
else if (choice2 == B);{
std::cout << “You are very sad and need to take action\n”;
std::cout << “Do you?\n”;
std::cout << “A. Indulge in beer and pizza for breakfast, lunch and dinner for six months?\n”;
std::cout << “B. Plan the next heist?\n”;
std::cout << “Enter your answer, A or B:”;
std::cin >> choice3;
}
if (choice3 == A){
std::cout << “You put on 6 stone, decide to take up Couch to 5K and become a vegan\n”;
}
else (choice3 == B);{
std::cout << “You capture Mars and set up a colony of Minions on the red planet\n”;
}
/*************************************/

else if (choice2 == C);{
std::cout << “”;
}
/*************************************/
else (choice1 == B); {
std::cout << "Vector, your arch-rival kidnaps the three orphans.\n ";
std::cout << “Do you?\n”;
std::cout << “A. Let the orphans go, they were a burden to your evil plans\n”;
std::cout << “B. Enter a battle with Vector, claiming back the orphans\n”;

  }

}
‘’’

You have a ; that does not belong on that line.
That is an problem for many of the else of else if lines of code in the code posted.

1 Like

Thanks! :slight_smile:

I removed all the extra ‘;’ in the else if statements too.

Not completely resolved the issue but reduced the number of error messages to 2.

I think the problem is that I’m trying to use nested IF statements and when I try to use ELSE IF statements that are linked to the original IF statement, that’s when I get the error messages.

You may be missing a } where the nesting ends.

You’ve wrapped your code in triple quotation marks(‘’') rather than triple back ticks. (```) Try posting your current code, properly formatted, along with the error messages you are receiving. The error messages will tell you where to look. Much easier and more efficient than proofreading your code over and over looking for typos or mistakes.

Hopefully this should work!

#include <iostream>
int main() {

 char choice1,choice2, choice3; 

 char A,B,C;

  std::cout << " You are one of the most villainous and grumpiest criminals of all time.\n ";
  std::cout << " You employ a small army of hapless yellow Minions dressed in blue outfits.\n ";
  std::cout << " You are planning to steal the Moon, the biggest heist in the history of the world!\n ";
  std::cout << " One day you encounter three orphans who see you as a potential Dad.\n ";
  std::cout << " You have met your greatest challenge yet.\n ";
  std::cout << " What do you do?\n ";

  std::cout << " Do you?\n"; 

  std:: cout << " A. Ignore the three orphans? OR \n"; 
  std::cout << "B. Decide to use them in your cunning plan to steal the Moon? \n";
  std::cout << " Enter your answer, A or B:";
  std::cin >> choice1;
  

  if (choice1 == A){
    std::cout << "You successfully steal the Moon!\n";
    std::cout << "Do you?\n";
    std::cout << "A. Live happily ever after knowing you've completed the biggest heist in the history of the world?\n";
    std::cout << "B. Feel regret and misery at ignoring the three orphans?\n";
    std::cout << "C. Go back to the orphanage and adopt the three girls anyway?\n";
    std::cout << "Enter your answer, A,B or C:";
    std::cin >> choice2;
  }
    else if (choice2 == A){
    std::cout << "Congratulations on capturing the Moon!\n";
    std::cout << "Do you?\n";
    std::cout << "A. Live a life of endless parties spending all your money until you're broke\n";
    std::cout << "B. Invest your time into self promotion so everyone knows you're the most successful villain ever\n";
    std::cout << "Enter your answer, A or B:";
    std::cin >> choice3;
  }
    else
        std::cout << "Incorrect";

        
      if (choice3 == A){
    std::cout << "You join a villains self-help group and become a goat herder\n";
  }
        else if(choice3 == B){
      std::cout << "You reach the ultimate upper echelons of infamy and spend the rest of your days handing out signed photos.\n";
    }
        else
        std::cout << "Incorrect";
      
/**************************************/
  else if (choice2 == B){
    std::cout << "You are very sad and need to take action\n";
    std::cout << "Do you?\n";
    std::cout << "A. Indulge in beer and pizza for breakfast, lunch and dinner for six months?\n";
    std::cout << "B. Plan the next heist?\n";
    std::cout << "Enter your answer, A or B:";
    std::cin >> choice3;
  }
      if (choice3 == A){
    std::cout << "You put on 6 stone, decide to take up Couch to 5K and become a vegan\n";
  }
      else (choice3 == B);{
      std::cout << "You capture Mars and set up a colony of Minions on the red planet\n";
    }
/*************************************/
 
  else if (choice2 == C){
    std::cout << "";
  }
/*************************************/
  else (choice1 == B); {
      std::cout << "Vector, your arch-rival kidnaps the three orphans.\n ";
      std::cout << "Do you?\n";
      std::cout << "A. Let the orphans go, they were a burden to your evil plans\n";
      std::cout << "B. Enter a battle with Vector, claiming back the orphans\n";
      
      
      }

}

Error messages;

'else' without a previous 'if'
 else if (choice2 == B) {
^~~~

and

'else' without a previous 'if'
 else if (choice2 == C) {
^~~~

please explain which choices are nested in which other choices so that I can understand what you’re trying to do.
Is the stuff for choice2 nested inside the stuff for choice1 ?
Is the stuff for choice3 nested inside the stuff for choice2 ?
Please explain what you want the structure to be.