Hi i am a beginner and am a little puzzled on how to make work of the fstream.My assignment says that i need to "use 1 read/write operation".I wanted to know if the information i have obtain throught the user from my code can be saved in a .txt file?And if yes using istream or ostream?
What I have tried:
This is my code,the class that i use to get the product,and later on wanting to save the print product in my list.txt i have.This is all i understand for now thank you for any help.
class Class
{
public:
int n , amount; string name; float weight;
vector <string> names;
vector <int> amounts;
vector <float> weights;
void getproducts()
{
for (int i = 0; i < n; i++)
{
cout << "Give product "<<i+1<<" name,amount and weight : " << endl;
cin >> name >> amount >> weight; names.push_back(name); amounts.push_back(amount);
weights.push_back(weight);
}
}
void printproducts()
{
int totali = 0;
cout << "\nProduct display:\n";
cout << endl;
for (int i = 0; i < n; i++)
{
cout << names[i] << " - " << amounts[i] << " , " << weights[i] << " kg" << endl;
cout << "------------------------" << endl;
totali = amounts[i] * weights[i] + totali;
}
cout << "Total: ";
cout << totali << " $ " << endl;
}
};
int main()
{
Class market;
market.getproducts(); market.printproducts();
ifstream inFile;
inFile.open("list.txt");
if (!inFile.fail())
{
cout << "Error in opening file." << endl;
exit(1);
}
cin.get(); cin.get();
return 0;
}