visual studio 2022 用数组设置一个水果入库表
时间: 2023-12-16 21:06:31 浏览: 24
可以使用以下代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
struct Fruit {
string name;
int quantity;
double price;
};
int main() {
const int NUM_FRUITS = 5;
Fruit inventory[NUM_FRUITS] = {
{"Apple", 10, 0.5},
{"Banana", 5, 0.3},
{"Orange", 8, 0.4},
{"Mango", 3, 1.0},
{"Pineapple", 2, 1.5}
};
// Print out the inventory table
cout << "Name\tQuantity\tPrice\n";
for (int i = 0; i < NUM_FRUITS; i++) {
cout << inventory[i].name << "\t" << inventory[i].quantity << "\t\t" << inventory[i].price << endl;
}
return 0;
}
```
这个程序中,我们定义了一个 `Fruit` 结构体,包含了水果的名称、数量和价格。然后我们定义了一个长度为 5 的数组 `inventory`,用来存储水果入库信息。在数组定义时,我们使用了初始化列表来初始化数组元素。最后,我们使用循环遍历数组,输出水果入库表格。