Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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;
}


----------
like image 566
Marc_L Avatar asked Oct 04 '13 21:10

Marc_L


2 Answers

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. "

like image 117
Niko Avatar answered Oct 12 '22 16:10

Niko


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";
}
// ...
like image 30
David G Avatar answered Oct 12 '22 17:10

David G