该程序允许多个葡萄酒输入,每个输入都需要放置到txt文件中,但是它只会将最后一个输入到txt文件中。
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
struct Wine1 {
string name;
string year;
string place;
string price;
} wine;
void printwine(Wine1 wine);
int main()
{
vector<Wine1> wineinventory;
// read 3 wines
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 << "enter country of creation: ";
getline(cin, wine.place);
cout << endl << "enter price: ";
getline(cin, wine.price);
cout << endl;
cout << "your entered data: " << endl;
printwine(wine);
wineinventory.push_back(wine); // store in vectore
}
// print all wines in the vector
for (int i = 0; i < wineinventory.size(); i++)
{
printwine(wineinventory[i]);
printwine(wine);
}
}
void printwine(Wine1 wine) { //data the user typed as output
ofstream file_;
file_.open("wine.txt");
file_ << "Wine" << endl;
file_ << "the name is: " << wine.name << endl;
file_ << "it's year is: " << wine.year << endl;;
file_ << "its country of creation is: " << wine.place << endl;;
file_ << "it's price is: " << wine.price << endl;
file_.close();
}
如摘要中所述,我需要程序将所有输入的葡萄酒类型打印到txt文件中,但是仅打印1。我已经浏览过类似的问题,但没有一个回答我的问题