0

Can't figure out why I have the error...The error is: C2181: illegal else without matching if....My program is:

#include <iostream>
using namespace std;

int main()
{
    int x=300; //this is the room capacity
    int y=0; //number of people
    int z=0; //this is the equation answer
    int m=0; //this is the number to be calculated

    cout << "Please enter the number of people who will be attending the meeting. \n" << endl;
    cin >> y;

    if (y <= x)
        cout << "This is a legal number of people to attend the meeting. \n" << endl;

    else
        cout << "The meeting cannot be held as planned due to fire regulations. \n" << endl;


    if (y <= x)
        z = x - y;
        cout << "There is enough room at the meeting for " << z << " more people too attend.\n" << endl;

    else
        cout << "In order to have the meeting " << y - x << " people need to leave the room.\n" << endl;

    return 0;

}

Edited by happygeek: fixed formatting

2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by WolfPack
0

Put a brace here.

if (y <= x)
{
z = x - y;
cout << "There is enough room at the meeting for " << z << " more people too attend.\n" << endl;
}
else
{
cout << "In order to have the meeting " << y - x << " people need to leave the room.\n" << endl;
}

It is good programming practice if you use braces for all the if else statements. Even if they have only one statement after the condition.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.