C++酒店营收程序循环异常:二次循环未正确累加楼层数据
酒店营收计算程序问题排查与修复
问题描述
我写了一个计算酒店营收的C++程序,里面有两个for (int a = 1; a <= floor; a++)循环。第一个循环能正常读取楼层数(比如输入2时会依次处理1、2层的房间数据),但第二个循环计算总营收、总房间数等统计数据时,没法累加各楼层的数据,只保留最后一层(第二层)的结果。
举个例子:输入floor=2,第一层参数是20间房(10间single、2间double、1间king、1间suite),第二层参数是10间房(5间single、1间double,king和suite为0),预期总营收1375美元、总房间30间、已入住20间,但实际输出是营收375美元、总房间10间、已入住6间。
原代码
#include <iostream> #include <string> #include <iomanip> using namespace std; int main() { string location; const int minFloor = 1, maxFloor = 5, minRoom = 1, maxRoom = 30; int floor, room, singleRoom, doubleRoom, kingRoom, suiteRoom; int singleRate = 60, doubleRate = 75, kingRate = 100, suiteRate = 150; double income, occupancyRate, total_of_rooms, occupiedRoom, unoccupiedRoom; cout << "\nEnter the total number of floors in the hotel: "; cin >> floor; // not more than 5 while (floor < minFloor || floor > maxFloor) { cout << "\nTne number of floors should be between 1 and 5 !! Please try again." << endl; cout << "\nEnter the total number of floors in the hotel: "; cin >> floor; } for (int a = 1; a <= floor; a++) { cout << "\nEnter total number of rooms in the " << a << "th Floor: "; cin >> room; //not more than 30 while (room < minRoom || room > maxRoom) { cout << "\nThe number of rooms should be between 1 and 30 !! Please try again." << endl; cout << "\nEnter total number of room in the " << a << "th Floor: "; cin >> room; } cout << "\nHow many SINGLE rooms are occupied on the " << a << "th Floor: "; cin >> singleRoom; cout << "How many DOUBLE rooms are occupied on the " << a << "th Floor: "; cin >> doubleRoom; cout << "How many KING rooms are occupied on the " << a << "th Floor: "; cin >> kingRoom; cout << "How many SUITE rooms are occupied on the " << a << "th Floor: "; cin >> suiteRoom; int totalRoom = singleRoom + doubleRoom + kingRoom + suiteRoom; while (totalRoom > room) //checking the sum { cout << "\nThe total number of occupied rooms exceeds the total number of rooms on this floor. Please try again !!"; cout << "\nHow many SINGLE rooms are occupied on the " << a << "th Floor: "; cin >> singleRoom; cout << "How many DOUBLE rooms are occupied on the " << a << "th Floor: "; cin >> doubleRoom; cout << "How many KING rooms are occupied on the " << a << "th Floor: "; cin >> kingRoom; cout << "How many SUITE rooms are occupied on the " << a << "th Floor: "; cin >> suiteRoom; int totalRoom = singleRoom + doubleRoom + kingRoom + suiteRoom; if (totalRoom < room) break; //break cause otherwise this thing won't stop } } for (int a = 1; a <= floor; a++) //for some reason it doesn't read the floor variable { income = (singleRoom * singleRate) + (doubleRoom * doubleRate) + (kingRoom * kingRate) + (suiteRoom * suiteRate); total_of_rooms = room; occupiedRoom = singleRoom + doubleRoom + kingRoom + suiteRoom; } cout << "Hotel Income: " << "$" << income << endl << "Total # of rooms: " << total_of_rooms << endl << "Total # of Occupied Rooms: " << occupiedRoom << endl }
问题原因
- 统计变量未初始化且未累加:
income、total_of_rooms、occupiedRoom这些统计变量没有初始化为0,而且第二个循环里是直接赋值(比如income = ...),不是累加(income += ...),每次循环都会把之前的数值覆盖,最后只保留最后一次循环的结果。 - 楼层数据未持久化:第一个循环里的
room、singleRoom等变量是全局的,每处理完一层,这些变量就会被下一层的数据覆盖,第二个循环根本拿不到之前楼层的数据,只能读取最后一层的数值。 - 第二个循环完全冗余:不需要单独开一个循环来统计,直接在第一个循环处理每层数据的时候,同步完成累加统计就可以了。
修复后的代码
#include <iostream> #include <string> #include <iomanip> using namespace std; int main() { string location; const int minFloor = 1, maxFloor = 5, minRoom = 1, maxRoom = 30; int floor, room, singleRoom, doubleRoom, kingRoom, suiteRoom; int singleRate = 60, doubleRate = 75, kingRate = 100, suiteRate = 150; // 初始化统计变量为0 double income = 0, occupancyRate, total_of_rooms = 0, occupiedRoom = 0, unoccupiedRoom; cout << "\nEnter the total number of floors in the hotel: "; cin >> floor; while (floor < minFloor || floor > maxFloor) { cout << "\nThe number of floors should be between 1 and 5 !! Please try again." << endl; cout << "\nEnter the total number of floors in the hotel: "; cin >> floor; } for (int a = 1; a <= floor; a++) { cout << "\nEnter total number of rooms in the " << a << "th Floor: "; cin >> room; while (room < minRoom || room > maxRoom) { cout << "\nThe number of rooms should be between 1 and 30 !! Please try again." << endl; cout << "\nEnter total number of room in the " << a << "th Floor: "; cin >> room; } cout << "\nHow many SINGLE rooms are occupied on the " << a << "th Floor: "; cin >> singleRoom; cout << "How many DOUBLE rooms are occupied on the " << a << "th Floor: "; cin >> doubleRoom; cout << "How many KING rooms are occupied on the " << a << "th Floor: "; cin >> kingRoom; cout << "How many SUITE rooms are occupied on the " << a << "th Floor: "; cin >> suiteRoom; int totalRoom = singleRoom + doubleRoom + kingRoom + suiteRoom; while (totalRoom > room) { cout << "\nThe total number of occupied rooms exceeds the total number of rooms on this floor. Please try again !!"; cout << "\nHow many SINGLE rooms are occupied on the " << a << "th Floor: "; cin >> singleRoom; cout << "How many DOUBLE rooms are occupied on the " << a << "th Floor: "; cin >> doubleRoom; cout << "How many KING rooms are occupied on the " << a << "th Floor: "; cin >> kingRoom; cout << "How many SUITE rooms are occupied on the " << a << "th Floor: "; cin >> suiteRoom; totalRoom = singleRoom + doubleRoom + kingRoom + suiteRoom; if (totalRoom <= room) break; } // 在第一个循环里直接累加统计数据 income += (singleRoom * singleRate) + (doubleRoom * doubleRate) + (kingRoom * kingRate) + (suiteRoom * suiteRate); total_of_rooms += room; occupiedRoom += singleRoom + doubleRoom + kingRoom + suiteRoom; } // 计算空置房间数和入住率(可选补充) unoccupiedRoom = total_of_rooms - occupiedRoom; occupancyRate = (occupiedRoom / total_of_rooms) * 100; cout << fixed << setprecision(2); cout << "\nHotel Income: $" << income << endl << "Total # of rooms: " << total_of_rooms << endl << "Total # of Occupied Rooms: " << occupiedRoom << endl << "Total # of Unoccupied Rooms: " << unoccupiedRoom << endl << "Occupancy Rate: " << occupancyRate << "%" << endl; return 0; }
修复说明
- 初始化所有统计变量为0,避免未定义行为。
- 删除冗余的第二个循环,在处理每层数据的第一个循环里直接完成累加操作(使用
+=替代=)。 - 修复了原代码中拼写错误(
Tne改为The)和循环内重复定义totalRoom的问题。 - 补充了空置房间数和入住率的计算,让输出更完整。
- 添加了
fixed和setprecision(2)保证金额和百分比的输出格式更规范。
内容的提问来源于stack exchange,提问作者Michelle
相关产品推荐
相关产品推荐

