如何制作一个结构程序,在其中可以存储无限量的数据,以便每次您想要时都可以将另一个产品添加到列表中?

How do I make a struct program in which I can store an infinite amount of data so that everytime you want you can add another product to the list?

本文关键字:都可以 另一个 列表 添加 数据 一个 结构 何制作 程序 在其中 无限      更新时间:2023-10-16

我有一个学校作业,我需要创建一个葡萄酒库存系统,使用它可以随时将不同的项目添加到列表中,例如,如果您在仓库之类的地方工作并且您获得需要放入系统的新产品,而不是更改代码,您只需将其输入控制台即可。

该程序可以工作,但是我只能添加3种不同类型的葡萄酒,因为我只制作了三种结构

#include <string>
#include <iostream>
using namespace std;
struct Wine1 {
string name;
string year;
string place;
string price;
} wine;
void printwine(Wine1 wine);
struct Wine2 {
string name;
string year;
string place;
string price;
} wine2;
void printwine2(Wine2 wine2);
struct Wine3 {
string name;
string year;
string place;
string price;
} wine3;
void printwine3(Wine3 wine3);

int main()
{
string str;
cout << "Please enter the data of the First wine: " << endl;
cout << "Enter name: " ;
getline(cin,wine.name);
cout << endl << "Enter year: ";
getline(cin, wine.year);
cout << endl << "enter country of creation: ";
getline(cin, wine.place);
cout << endl << "enter price: ";
getline(cin, wine.price);
cout << endl;
cout << "Please enter the data of the Second wine: " << endl;
cout << "Enter name: ";
getline(cin, wine2.name);
cout << endl << "Enter year: ";
getline(cin, wine2.year);
cout << endl << "enter country of creation: ";
getline(cin, wine2.place);
cout << endl << "enter price: ";
getline(cin, wine2.price);
cout << endl;
cout << "Please enter the data of the third wine: " << endl;
cout << "Enter name: ";
getline(cin, wine3.name);
cout << endl << "Enter year: ";
getline(cin, wine3.year);
cout << endl << "enter country of creation: ";
getline(cin, wine3.place);
cout << endl << "enter price: ";
getline(cin, wine3.price);
cout << endl;
cout << "your entered data: " << endl;
printwine(wine);
cout << endl;
printwine2(wine2);
cout << endl;
printwine3(wine3);
}
void printwine(Wine1 wine) {
cout << "Wine1" << endl;
cout << "the name is: " << wine.name << endl;
cout << "it's year is: " << wine.year << endl;;
cout << "its country of creation is: " << wine.place << endl;;
cout << "it's price is: " << wine.price << endl;
}
void printwine2(Wine2 wine2) {
cout << "Wine2" << endl;
cout << "the name is: " << wine2.name << endl;
cout << "it's year is: " << wine2.year << endl;;
cout << "its country of creation is: " << wine2.place << endl;;
cout << "it's price is: " << wine2.price << endl;;
}
void printwine3(Wine3 wine3)
{
cout << "Wine3" << endl;
cout << "the name is: " << wine3.name << endl;
cout << "it's year is: " << wine3.year << endl;;
cout << "its country of creation is: " << wine3.place << endl;
cout << "it's price is: " << wine3.price << endl;;
}

您应该注意到您正在创建 3 个相同的结构。

因此,这可以通过结构向量或数组轻松完成。

我会声明:vector<Wine> wines(3);其中3是您将拥有的葡萄酒数量。

并输入您可以简单地使用的值:wines[i].name其中"i"是您要编辑的葡萄酒。

我想你已经知道循环了,因为这是你必须使用的。

此外,如果要使用向量,则必须将其包含在#include <vector>

相关文章: