Looping data into a output file

I'm not sure how to continuously loop my void function so that each time information is inputted into the main it can continuously print it out onto the output text. Example after running the program once, it asks if i want to run it again if i enter yes the old information is erased from the output text and the new information is replaced.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <fstream> // Used to output into a text file 
#include <iomanip>

using namespace std;

struct programParts
{
	string partNumber; 
	string partName;
	char partClass;
	int numStock;
	double unitPrice;	
};

void writeOut(string number, string name, char c, int stock, double price);// Function Prototype

int main()
{
	// Variables needed 
	string number;
	string name;
	char c;
	int stock;
	double price;
	char choice;


	do
	{
		cout << "Enter part information (part number, name, class, on hand balance and price):" << endl;
		cin >> number >> name >> c >> stock >> price;
		programParts details;
		writeOut(number, name, c, stock, price);

		cout << "More parts? (y/n)" << endl;
		cin >> choice;


	} 
	while (choice == 'y'); // User input, continues the loop 
		cout << "Goodbye!" << endl;

}

void writeOut(string number, string name, char c, int stock, double price)
{
	ofstream fout("out.txt");
	fout << number << "  " << name << "  " << c << "  " << stock << "  " << price << endl;

}
I would open it in main and pass it by reference to the function. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

void toFile(const partInfo &part, ofstream &fout);

int main()
{

	ofstream fout;

	//open file
	fout.open("parts.txt");

	//check it opened
	if (fout.fail())
	{
		cout << "Error opening output file!" << endl;
	}
	else
	{
		//code



		//call function
		toFile(part, fout);
	}

	//close file
	fout.close();
}

void toFile(const partInfo &part, ofstream &fout)
{
	//set 2 decimal places
	cout << fixed << setprecision(2);

	//output to file
	fout << part.partNum << " " << part.name << " " << part.partClass << " " << part.numOnHand << " " << part.price << endl;
}
Thank you @joe864864!!
Topic archived. No new replies allowed.