用C++写一个酒店入住的增删改查
时间: 2023-10-04 12:07:11 浏览: 81
好的,以下是一个简单的酒店入住管理系统的增删改查的C++实现:
```c++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 定义一个酒店房间的结构体
struct Room {
int roomNumber; // 房间号
string guestName; // 入住客人姓名
int guestNumber; // 入住客人人数
};
vector<Room> roomList; // 定义一个酒店房间的列表
// 添加房间
void addRoom() {
Room room;
cout << "请输入房间号:" << endl;
cin >> room.roomNumber;
cout << "请输入入住客人姓名:" << endl;
cin >> room.guestName;
cout << "请输入入住客人人数:" << endl;
cin >> room.guestNumber;
roomList.push_back(room);
cout << "添加房间成功!" << endl;
}
// 删除房间
void deleteRoom() {
int roomNumber;
cout << "请输入要删除的房间号:" << endl;
cin >> roomNumber;
for (vector<Room>::iterator it = roomList.begin(); it != roomList.end(); it++) {
if (it->roomNumber == roomNumber) {
roomList.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "该房间不存在!" << endl;
}
// 修改房间信息
void modifyRoom() {
int roomNumber;
cout << "请输入要修改的房间号:" << endl;
cin >> roomNumber;
for (vector<Room>::iterator it = roomList.begin(); it != roomList.end(); it++) {
if (it->roomNumber == roomNumber) {
cout << "请输入新的入住客人姓名:" << endl;
cin >> it->guestName;
cout << "请输入新的入住客人人数:" << endl;
cin >> it->guestNumber;
cout << "修改成功!" << endl;
return;
}
}
cout << "该房间不存在!" << endl;
}
// 查询房间信息
void queryRoom() {
int roomNumber;
cout << "请输入要查询的房间号:" << endl;
cin >> roomNumber;
for (vector<Room>::iterator it = roomList.begin(); it != roomList.end(); it++) {
if (it->roomNumber == roomNumber) {
cout << "房间号:" << it->roomNumber << endl;
cout << "入住客人姓名:" << it->guestName << endl;
cout << "入住客人人数:" << it->guestNumber << endl;
return;
}
}
cout << "该房间不存在!" << endl;
}
int main() {
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加房间" << endl;
cout << "2. 删除房间" << endl;
cout << "3. 修改房间信息" << endl;
cout << "4. 查询房间信息" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
addRoom();
break;
case 2:
deleteRoom();
break;
case 3:
modifyRoom();
break;
case 4:
queryRoom();
break;
default:
cout << "输入有误,请重新输入!" << endl;
}
}
return 0;
}
```
使用方法:
1. 编译运行程序
2. 选择需要的操作,根据提示进行操作即可
注意:本程序只是一个简单的示例,实际应用中还需要进行更多的错误处理和数据验证。
阅读全文