Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do fix the error: expected unqualified-id before ‘if’ in my game

+1 vote
asked Apr 21 by Hayden Dorf (130 points)

I'm trying to make a game but at the part where you choose it keeps saying there is a 

error: expected unqualified-id before ‘if’

can you Please help me (ps the game is called 80 day around the world) also here is the code  (pss the problom is with the first if)

#include <iostream>

using namespace std;

int main () 

{

   

   

   

    cout << "Do You Want To Play 80 Days Around The World?";

cout << "\n B) Yes"; // making the question

cout << "\n C) No";

cout << "\n D) Maybe";

cout << "\n";

char a,

    b,

    c,

          d,

      

       if (cin >> b) 

         {

         cout << "Yay";

         }

         if (cin >> c) 

         {

         cout << "Aww";

         }

         else 

         {

         cout << "well...";

         }

   

  

  

  

    return 0;

}

1 Answer

0 votes
answered Apr 27 by Peter Minarik (40,320 points)

This is how you read (string) from the standard input: cin >> answer;

Then you can compare it against the expected results: if (answer == "a")

When you declared your variables, it should end with a semicolon, which you were missing. Instead you had another comma after d.

char a, b, c, d;

Last but not least, when you check for the values b, c and d, you should do it in a series of if-else otherwise multiple results will be displayed.

Here's the whole code fixed:

#include <iostream>
#include <string>

using namespace std;

int main () 
{
    cout << "Do You Want To Play 80 Days Around The World?";
    cout << "\n B) Yes"; // making the question
    cout << "\n C) No";
    cout << "\n D) Maybe";
    cout << "\n";
    string answer;
    cin >> answer;
    if (answer == "b") 
    {
        cout << "Yay";
    }
    else if (answer == "c") 
    {
        cout << "Aww";
    }
    else
    {
        cout << "well...";
    }

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...