嵌套的 if-else 语句可能不起作用
Nested if-else statement not working probably
我需要一些有关该程序的帮助。当人数大于房间允许的最大人数时,最后的语句不打印。我不确定我是否做错了什么,或者错过了重要的项目。根据我使用的文本,我认为我不需要在最后一个 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;
}
在您的 else-if 语句中,您没有比较两个变量,而是将 numberOfPeople
分配给了 maxRoomCapacity
。赋值计算结果为真,导致执行 if-else 的主体,导致程序流跳过 else
语句。
问题在这里:
else if (maxRoomCapacity = numberOfPeople)
改为:
else if (maxRoomCapacity == numberOfPeople)
注意:
=
是赋值运算符
==
是比较运算符
编译警告(例如 -Wall
for 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)
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
我需要一些有关该程序的帮助。当人数大于房间允许的最大人数时,最后的语句不打印。我不确定我是否做错了什么,或者错过了重要的项目。根据我使用的文本,我认为我不需要在最后一个 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;
}
在您的 else-if 语句中,您没有比较两个变量,而是将 numberOfPeople
分配给了 maxRoomCapacity
。赋值计算结果为真,导致执行 if-else 的主体,导致程序流跳过 else
语句。
问题在这里:
else if (maxRoomCapacity = numberOfPeople)
改为:
else if (maxRoomCapacity == numberOfPeople)
注意:
=
是赋值运算符==
是比较运算符
编译警告(例如 -Wall
for 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)
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~