Возможно, не работает вложенное выражение if-else

Мне нужна помощь с программой. Когда число людей превышает максимально допустимое количество человек в комнате, последнее утверждение не печатается. Я не уверен, что сделал что-то не так или пропустил важный предмет. Я не думаю, что мне нужно включать логическое выражение в последний оператор else в соответствии с текстом, который я использую, поэтому я не использовал там логическое выражение
Пожалуйста помогите. Спасибо за вашу помощь

//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;
}

-6

Решение

В своем выражении else-if вместо сравнения двух переменных вы назначили numberOfPeople в maxRoomCapacity, Присвоение оценивается как true, в результате чего тело этого if-else выполняется, в результате чего поток вашей программы пропускает else заявление.

Проблема здесь:

else if (maxRoomCapacity = numberOfPeople)

измените это на:

else if (maxRoomCapacity == numberOfPeople)

Заметить, что:

  • = является оператором присваивания
  • == является оператором сравнения

Компилировать с предупреждениями (например, -Wall для GCC), и вы должны получить:

prog.cc: In function 'int main()':
prog.cc:26:26: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
else if (maxRoomCapacity = numberOfPeople)
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
2

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector