顾客信息的记录和查询可以通过一个顾客信息文件来实现。每次交易时,可以让用户输入顾客信息,并记录到文件中。查询功能可以根据顾客姓名、电话等信息进行筛选,并输出符合条件的顾客信息。(C++)
时间: 2024-02-03 13:12:43 浏览: 19
以下是一个简单的顾客信息记录和查询的 C++ 代码示例:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 定义顾客结构体
struct Customer {
string name;
string phone;
string address;
};
// 将顾客信息写入文件
void writeToFile(Customer customer) {
ofstream file("customers.txt", ios::app);
file << customer.name << "," << customer.phone << "," << customer.address << endl;
file.close();
cout << "顾客信息已保存" << endl;
}
// 根据姓名或电话查询顾客信息
void search(string keyword) {
ifstream file("customers.txt");
string line;
while (getline(file, line)) {
size_t pos = line.find(keyword);
if (pos != string::npos) {
cout << line << endl;
}
}
file.close();
}
int main() {
int choice;
cout << "请选择操作:" << endl;
cout << "1. 添加顾客信息" << endl;
cout << "2. 查询顾客信息" << endl;
cin >> choice;
if (choice == 1) {
Customer customer;
cout << "请输入顾客姓名:" << endl;
cin >> customer.name;
cout << "请输入顾客电话:" << endl;
cin >> customer.phone;
cout << "请输入顾客地址:" << endl;
cin >> customer.address;
writeToFile(customer);
} else if (choice == 2) {
string keyword;
cout << "请输入查询关键字(姓名或电话):" << endl;
cin >> keyword;
search(keyword);
} else {
cout << "输入错误!" << endl;
}
return 0;
}
```
代码中使用了一个 `Customer` 结构体来保存顾客信息,并将信息以逗号分隔的形式写入 `customers.txt` 文件。查询功能使用了 `ifstream` 类来读取文件,并使用 `find` 函数在每一行中查找关键字。需要注意的是,这个示例代码只是一个简单的实现,实际应用中还需要考虑更多的异常情况和错误处理。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)