If else statement won't work.

No matter what I put in for "room" it outputs "the number of students exceeds the limit." Could someone explain why it is doing that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>

using namespace std;

int main()
{
  const int LH312 = 42;
  const int CAS41 = 44;
  const int KH133 = 38;
  int room;
  int amountOfPeople;
  {//Gathers room number and amount of people
      cout << "Choose a room" << endl;
      cout << "1. Leigh Hall Room 312" << endl;
      cout << "2. College of Arts and Sciences Room 41" << endl;
      cout << "3. Kolbe Hall Room 133" << endl;
      cout << "Option:";
      cin >> room;
      cout << "How many people are in the room?" << endl << "Number of People:";
      cin >> amountOfPeople;
  }
  if (room == 1)
  {
      if (amountOfPeople <= LH312)
      {
         cout << "It is legal to hold your class";
      }
      else (amountOfPeople > LH312);
      {
          cout << "The amount of students exceeds the limit";
      }
  }
  else if (room == 2)
  {
      if (amountOfPeople <= CAS41)
      {
          cout << "It is legal to hold your class";
      }
      else (amountOfPeople > CAS41);
      {
          cout << "The amount of students exceeds the limit";
      }
  }
  else (room == 3);
  {
      if (amountOfPeople <= KH133)
      {
          cout << "It is legal to hold your class";
      }
      else (amountOfPeople > KH133);
      {
          cout << "The amount of students exceeds the limit";
      }
  }
    return 0;
}
The else part of an if statement should not have a condition.

 
else (amountOfPeople > LH312);

Thanks that partially solved my problem. For some reason the if-else outputs the text twice.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>

using namespace std;

int main()
{
  const int LH312 = 42;
  const int CAS41 = 44;
  const int KH133 = 38;
  int room;
  int amountOfPeople;
  {//Gathers room number and amount of people
      cout << "Choose a room" << endl;
      cout << "1. Leigh Hall Room 312" << endl;
      cout << "2. College of Arts and Sciences Room 41" << endl;
      cout << "3. Kolbe Hall Room 133" << endl;
      cout << "Option:";
      cin >> room;
      cout << "How many people are in the room?" << endl << "Number of People:";
      cin >> amountOfPeople;
  }
  if (room == 1)
  {
      if (amountOfPeople <= LH312)
      {
         cout << "It is legal to hold your class";
      }
      else
      {
          cout << "The amount of students exceeds the limit";
      }
  }
  else if (room == 2)
  {
      if (amountOfPeople <= CAS41)
      {
          cout << "It is legal to hold your class";
      }
      else
      {
          cout << "The amount of students exceeds the limit";
      }
  }
  else (room == 3);
  {
      if (amountOfPeople <= KH133)
      {
          cout << "It is legal to hold your class";
      }
      else
      {
          cout << "The amount of students exceeds the limit";
      }
  }
    return 0;
}
That didn't work. I put that semi-colon there because the following line gets the error: "expected ';' before '{' token."
That was the issue. I thought you were supposed to use "else" before the last condition instead of "else if." Thank you for the help.
You could add another else if you want to print something in case the user inputs a number that is not equal to one of 1, 2 or 3.
1
2
3
4
5
6
7
8
9
...
else if (room == 3)
{
	...
}
else
{
	cout << "Sorry, we don't have such a room.";
}
Why you didn't use Switch-case? Its faster and more readable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
switch(room){
case 1:
    if (amountOfPeople <= LH312)
        cout << "It is legal to hold your class";
    else
        cout << "The amount of students exceeds the limit";
    break;

case 2:
    if (amountOfPeople <= CAS41)
        cout << "It is legal to hold your class";
    else
        cout << "The amount of students exceeds the limit";
    break;

case 3:
    if (amountOfPeople <= KH133)
        cout << "It is legal to hold your class";
    else
        cout << "The amount of students exceeds the limit";
    break;
default:
    cout<<"The input was invalid!"<<endl;
}
Topic archived. No new replies allowed.