我正在为那些希望通过简单方式查看自己拥有的葡萄酒的人建立葡萄酒库存系统,该系统会自动获取其市场价格和创建国家。
该程序应该从下面的代码中看到的多个预定义结构中获取结构,用户只需输入名称和年份,即可打印整个结构,例如,我键入“ Greenock creek Roennfelt road shiraz ” em>”和年份“ 2002 ”,然后输出如下所示的整个结构。 我当时在考虑使用读取或获取命令,但在研究如何进行此操作之前,我想问一问是否有更有效的方法。
以下结构只是连接到主文件的第二个c ++文件中大量预定结构中的许多结构之一。
如果可以的话,这在c ++中是否可行?
另一个文件中的结构:
struct red1 // one of the predetermined structs
{
string name = "Greenock Creek Roennfeldt Road Shiraz";
double year = 2002;
string place = "Australia";
double price = 295.00;
string type = "Redwine";
};
主文件输入:(这部分不是100%,但这只是为了显示我的意思。
for (int i = 3; i < 10; i++)
{
string str; //input for data
cout << "Please enter the data of your Wine: " << endl;
cout << "Enter name: ";
getline(cin, wine.name);
cout << endl << "Enter year: ";
getline(cin, wine.year);
cout << endl;
cout << "your entered data: " << endl;
printwine(wine);
wineinventory.push_back(wine); // store in vector
}
答案 0 :(得分:3)
我不明白您为什么要具有多个结构。我认为您只需要一个,然后为不同的葡萄酒创建不同的实例。为了便于说明,我将仅使用年份和名称:
#include <vector>
#include <string>
#include <iostream>
struct wine {
int year;
std::string name;
};
// custom output operator to insert a wine into a ostream
std::ostream& operator<<(std::ostream& out, const wine& w) {
out << "year: " << w.year << " " << w.name;
return out;
};
int main() {
// fill a vector with different wines
std::vector<wine> wines { {2001,"the red one"}, {2005,"the white one"}};
// select a year
int year = 2001;
// pick the ones with matching year and print them
for (auto& w : wines) {
if (w.year == year) std::cout << w << "\n";
}
}
这将打印:
year: 2001 the red one
答案 1 :(得分:1)
已经有很多支持的答案。很好。
我只是想以正确的方向指导新用户。由于我们正在使用C ++,因此我们应该使用一种更加面向对象的方法。
您有数据,并且有一些应该对数据起作用的方法。例如,您的Wine对象具有属性,即数据成员。并且只有Wine对象应该对它们的成员进行操作。因此,我添加/重载了插入器和提取器功能。插入程序知道如何打印其数据。以后,您甚至可以封装数据,除了函数之外,其他任何人都不能对其进行操作。如果以后将一个属性添加到Wine中,则将修改插入器,程序的其余部分将继续工作。您需要进行此抽象。
因此,我建议学习面向对象的方法,否则,您将继续使用一些语法C ++糖编写C代码。
我为您起草了一个框架示例程序。它应该让您了解我的意思
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <iterator>
// A wine with its data and methods
struct Wine
{
std::string name{};
int year{};
std::string place{};
double price{};
std::string type{};
friend std::istream& operator >> (std::istream& is, Wine& w) {
return is >> w.name >> w.year >> w.place >> w.price >> w.type;
}
friend std::ostream& operator << (std::ostream& os, const Wine& w) {
return os << w.name << "\n" << w.year << "\n" << w.place << "\n" << w.price << "\n" << w.type << "\n";
}
};
// A Wine list (internally a vector) with its data and methods
class Wines
{
std::vector<Wine> wines{};
public:
void add(Wine& wine) { wines.push_back(wine); }
void remove(std::string wineName) { wines.erase(std::remove_if(wines.begin(), wines.end(), [&wineName](const Wine & w) { return w.name == wineName; }), wines.end()); }
bool findAndPrint(std::string& wineName, std::ostream& os) {
bool result = false;
std::vector<Wine>::iterator found = std::find_if(wines.begin(), wines.end(), [&wineName](const Wine & w) { return w.name == wineName; });
if (found != wines.end()) {
os << "\nWine found:\n" << *found;
result = true;
}
else
os << "\nNo wine with this name found\n";
return result;
}
friend std::istream& operator >> (std::istream& is, Wines& w) {
w.wines.clear();
std::copy(std::istream_iterator<Wine>(is), std::istream_iterator<Wine>(), std::back_inserter(w.wines));
return is;
}
friend std::ostream& operator << (std::ostream& os, const Wines& w) {
std::copy(w.wines.begin(), w.wines.end(), std::ostream_iterator<Wine>(os, "\n"));
return os;
}
};
int main(void)
{
// One wine
Wine wine;
// A lsit with wines
Wines wines;
// Add some data
std::cout << "\nEnter wine data. Name, Year, Place, Price, Type:\n";
std::cin >> wine;
wines.add(wine);
std::cout << "\n\nEnter another wine data. Name, Year, Place, Price, Type:\n";
std::cin >> wine;
wines.add(wine);
{
// Store all wines on disk
std::cout << "\nSaving on disk\n\n";
std::ofstream database("c:\\temp\\winelist.txt");
// Stores all wines in file
if (database) database << wines;
}
{
// Read back all wines from disk
std::cout << "\nReading from disk\n\n";
std::ifstream database("c:\\temp\\winelist.txt");
// Reads the complete list from file
if (database) database >> wines ;
}
// Search for a wine, if found, then remove it
std::cout << "\n\nWine List:" << wines << "\n\n\nSearch for a wine. Enter a wine name:\n" << wines;
std::string wineToSearch;
std::cin >> wineToSearch;
if (wines.findAndPrint(wineToSearch, std::cout)) {
wines.remove(wineToSearch);
std::cout << "\nRemoving wine from list: New List\n\n" << wines << "\n";
}
return 0;
}
当然还有很多其他可能性。但是你应该明白这个主意。