按订单编号进行查询函数c++
时间: 2023-11-27 17:51:15 浏览: 87
以下是一个简单的C++函数,它可以按照订单编号查询订单:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Order {
string order_number;
string customer_name;
string product_name;
};
vector<Order> orders;
void search_order(string order_number) {
bool found = false;
for (int i = 0; i < orders.size(); i++) {
if (orders[i].order_number == order_number) {
cout << "Order Number: " << orders[i].order_number << endl;
cout << "Customer Name: " << orders[i].customer_name << endl;
cout << "Product Name: " << orders[i].product_name << endl;
found = true;
break;
}
}
if (!found) {
cout << "Order not found!" << endl;
}
}
int main() {
// add some sample orders
orders.push_back({"1001", "John Smith", "iPhone 12"});
orders.push_back({"1002", "Jane Doe", "Samsung Galaxy S21"});
orders.push_back({"1003", "Bob Johnson", "Google Pixel 5"});
// search for an order by order number
string order_number;
cout << "Enter order number to search: ";
cin >> order_number;
search_order(order_number);
return 0;
}
```
该程序首先定义了一个表示订单的结构体 `Order`,其中包含订单号、客户姓名和产品名称。然后,它定义了一个 `vector`,用于存储所有订单。在 `main` 函数中,程序添加了一些示例订单,并然后提示用户输入要查询的订单号。最后,程序调用 `search_order` 函数来查找指定的订单,并显示订单的详细信息。如果订单不存在,则输出一条错误消息。
阅读全文