用C++语言完成下面这个编程:有一个售货柜员机,内有多种商品供人购买(商品种类不超过100个),每种商品有 名称、数量、价等信息。编程完成n种商品信息的输入输出。
时间: 2024-02-22 17:59:58 浏览: 20
好的,下面是使用 C++ 完成您的要求的代码示例:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 定义商品类
class Product {
public:
string name;
int quantity;
float price;
};
int main() {
// 初始化商品列表
Product products[100];
int n;
// 输入n种商品信息
cout << "请输入商品种类数量:";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "请输入第" << i+1 << "种商品名称:";
cin >> products[i].name;
cout << "请输入第" << i+1 << "种商品数量:";
cin >> products[i].quantity;
cout << "请输入第" << i+1 << "种商品价格:";
cin >> products[i].price;
}
// 输出商品信息
cout << "商品列表:" << endl;
for (int i = 0; i < n; i++) {
cout << "第" << i+1 << "种商品:" << endl;
cout << "名称:" << products[i].name << endl;
cout << "数量:" << products[i].quantity << endl;
cout << "价格:" << products[i].price << endl;
}
return 0;
}
```
以上代码中,我们使用 `Product` 类来定义商品对象,使用数组 `products` 存储所有商品信息,使用变量 `n` 存储商品种类数量。通过 `cin` 和 `cout` 来实现输入和输出。