Yes/No questions with If/Else and char

Hi!

Obviously Im pretty new to this but I would really appreciate some help.

My assignment is to create a program with yes n no answers using if/else statements and char. So these I cannot change.

Program is supposed to ask the user if the weather is nice or not.
If yes print out: Lets have picnic
If n lets: Stay inside and read a book
And if anything else is put in its supposed to say: I dont understand.

First issue Im having is that the y/n gets printed out no matter what I do. . .

2nd issues is that everything works fine until I add the last else statement and then it wont run with out error code: expected unqualified- id before 'else'

I have search for awhile regarding if/else and char but unfortunately not finding any answers that fits for my assignment.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>

using namespace std;

int main()
{
    cout << "Is the weather nice? y/n"; // y/n gets printed out
    char answer;
    cin >> answer;

    if (answer == 'y' || answer == 'Y')
    {
        cout << "Lets have a picknick!";
    }
    else //not working
    {
        cout << "Lets stay inside and read a book";
    }
}
else
{
    cout << "I dont understand!";

}


So if anyone can please help explain this to me, I would appreciate it!!!!
An else statement must be connected to an if() statement and there can only be one else statement to a particular if() statement.

The closing brace at line 19 matches the opening brace at line 6. They mark the start and end of the main() function. Hence lines 20 - 24 are not part of the program, they are floating about on their own.

As for the description,
If yes print out: Lets have picnic
If n lets: Stay inside and read a book
at line 11 the program checks for the letter 'Y'.

But there is no test for the letter 'N'. You need to follow the first else with another if.

It is usual to do that something like this:
1
2
3
4
5
6
7
8
9
10
11
12
if (condition)
{
    ....
}
else if (another condition)
{
    ...
}
else
{
    ...
}
Topic archived. No new replies allowed.