如何制作一个结构程序,在其中可以存储无限量的数据,以便每次您想要时都可以将另一个产品添加到列表中?
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?
我有一个学校作业,我需要创建一个葡萄酒库存系统,使用它可以随时将不同的项目添加到列表中,例如,如果您在仓库之类的地方工作并且您获得需要放入系统的新产品,而不是更改代码,您只需将其输入控制台即可。
该程序可以工作,但是我只能添加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>
相关文章:
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- C++代码以测试另一个.cpp如果可以编译它
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 是否可以使用一个类来控制 C++ 中另一个类的对象?(阿杜伊诺)
- 如果需要转换,我可以在读取参数的同时将其移动到另一个参数吗?
- 是否可以使用分配器对象来释放另一个分配器分配的内存?
- 是否可以使用智能指针成员设置具有另一个结构的结构?
- 为什么我可以使用 memcpy 将一个对象变量复制到另一个对象变量
- 如果类"A"具有指向另一个类的指针,则"B" A 可以访问 B 类公共方法
- 在C++中,是否可以"overload"作为另一个类成员的对象?
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?
- 如何在 c++ 中实现 avl 树,每个节点都是另一个 avl 树
- 如何制作一个结构程序,在其中可以存储无限量的数据,以便每次您想要时都可以将另一个产品添加到列表中?
- 我所有应用程序类都可以访问一个对象
- 我想使用C++映射,以存储一个矩形id的4个坐标,任何人都可以举一个例子吗
- 如果我把这些都放到另一个函数里,是不是很好
- 是他们编译一个项目的一种方式,这样你的所有函数类都可以.进入一个可以链接的文件