[c++] error : expected unqualified-id before return in c++

when I want to compile I get : Probléme : expected unqualified-id before "return" return 0; about last line : erreur:expexted declaration before { token

I left the code unchanged just the middle part I changed... whats the problem??? here is my code:


#include <iostream>
using namespace std;

int main()
{
  cout << "Pensez a un personnage : Mlle Rose, le Professeur Violet, "
       << "le Colonel Moutarde," << endl
       << "le Reverend Olive ou Mme Leblanc." << endl << endl;

  cout << "Votre personnage a-t-il des moustaches (1 : oui, 0 : non) ? ";
  bool moustaches;
  cin >> moustaches;

  cout << "Votre personnage porte-t-il des lunettes ? ";
  bool lunettes;
  cin >> lunettes;

  cout << "Votre personnage porte-t-il un chapeau ? ";
  bool chapeau;
  cin >> chapeau;

  cout << "Est-ce que votre personnage est un homme ? ";
  bool homme;
  cin >> homme;

  cout << "==> Le personnage auquel vous pensez est ";

  if (chapeau) {
    /*******************************************
     * Completez le programme a partir d'ici.
     *******************************************/
    cout << "le Professeur Violet";

    else if (moustaches) {
        cout << "le Colonel Moutarde";
    }
    else if (not lunettes) {
        cout << "Mlle Rose";
    }
    else if (homme) {
        cout <<"le Révérend Olive";
    }
    else {
        cout <<"Mme Leblanc";
    }

    /*******************************************
     * Ne rien modifier apres cette ligne.
     *******************************************/
  }

  cout << endl;

  return 0;
}


----------

This question is related to c++

The answer is


if (chapeau) {

You forgot the ending brace to this if statement, so the subsequent else if is considered a syntax error. You need to add the brace when the if statement body is complete:

if (chapeau) {
    cout << "le Professeur Violet";
}
else if (moustaches) {
    cout << "le Colonel Moutarde";
}
// ...

Suggestions:

  • use consistent 3-4 space indenting and you will find these problems much easier
  • use a brace style that lines up {} vertically and you will see these problems quickly
  • always indent control blocks another level
  • use a syntax highlighting editor, it helps, you'll thank me later

for example,

type
functionname( arguments )
{
    if (something)
    {
        do stuff
    }
    else
    {
        do other stuff
    }
    switch (value)
    {
        case 'a':
            astuff
            break;
        case 'b':
            bstuff
            //fallthrough //always comment fallthrough as intentional
        case 'c':
            break;
        default: //always consider default, and handle it explicitly
            break;
    }
    while ( the lights are on )
    {
        if ( something happened )
        {
            run around in circles
            if ( you are scared ) //yeah, much more than 3-4 levels of indent are too many!
            {
                scream and shout
            }
        }
    }
    return typevalue; //always return something, you'll thank me later
}

You need to move " } " before the line of cout << endl; to the line before the first else .


Just for the sake of people who landed here for the same reason I did:

Don't use reserved keywords

I named a function in my class definition delete(), which is a reserved keyword and should not be used as a function name. Renaming it to deletion() (which also made sense semantically in my case) resolved the issue.

For a list of reserved keywords: http://en.cppreference.com/w/cpp/keyword

I quote: "Since they are used by the language, these keywords are not available for re-definition or overloading. "