-6

i need some assistance with the program. When the number of people is greater than the maximum number of people allowed in the room, the last statement does not print. I am not sure if i did something wrong, or missed an important item. I don't think i need to include a Boolean expression in the last else statement according to the text i am using, this is why i did not use any boolean expression there Please assist. Thanks for your help

//Write a program that determines whether a meeting room is in violation of fire law regulations regarding the maximum room capacity. 
//The program will read in the maximum room capacity and the number of people to attend the meeting. If the number of people is less than 
//or equal to the maximum room capacity, the program announces that it is legal to hold the meeting and tells how many additional people 
//may legally attend. If the number of people exceeds the maximum room capacity, the program announces that the meeting cannot be held as 
//planned due to fire regulations and tells how many people must be excluded in order to meet the fire regulations.

#include <iostream>

using namespace std;

int main()
{
//variable declaration
int numberOfPeople, maxRoomCapacity, morePeople, lessPeople;

//program ask user of input
cout << "Enter the number of people to attend the meeting: ";
cin >> numberOfPeople;
cout << "What is the room capacity: ";
cin >> maxRoomCapacity;

//formula to calculate the number of people that meets fire regulation
morePeople = maxRoomCapacity - numberOfPeople;
lessPeople = numberOfPeople - maxRoomCapacity;

//if-else statement to determine if fire regulation is met
if (numberOfPeople < maxRoomCapacity)
{
    cout << "It is legal to hold the meeting in the room, plus " << morePeople
        << " additional people may legally attend the meeting." << endl;
}
else if (maxRoomCapacity = numberOfPeople)
{
    cout << "It is legal to hold the meeting in the room, no additional person can be allowed." << endl;
}
else
{
    cout << "This meeting cannot be held as planned due to fire regulations. "
        << lessPeople << " people must be excluded in order to meet the fire regulations." << endl;
}

system("pause");
return 0;
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
leet
  • 9
  • 3

1 Answers1

2

In your else-if statement, instead of comparing the two variables, you assigned numberOfPeople to maxRoomCapacity. The assignment evaluates to true, causing the body of that if-else to execute, causing the flow of your program to skip the else statement.

The problem is here:

else if (maxRoomCapacity = numberOfPeople)

change it to:

else if (maxRoomCapacity == numberOfPeople)

Notice that:

  • = is an assignment operator
  • == is a comparison operator

Compile with warnings (e.g. -Wall for GCC), and you should get:

prog.cc: In function 'int main()':
prog.cc:26:26: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
 else if (maxRoomCapacity = numberOfPeople)
          ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    Thank you so much @gsamaras. I forgot the difference between assignment operator and comparison operator. I am so sorry for bothering you all. I am a newbie. Thanks again. – leet Sep 20 '17 at 06:13
  • Oh, i didn't know. Just accepted it. Thanks – leet Sep 20 '17 at 06:23