Repetition of output in a text file.

So, I wanted to do some practice around fstream, and I decided to make a sort of database where data regarding voters read from a txt file is sorted into two files based on age. Then, the program reads the files with ages over 18, and counts the votes for each political party. I haven't reached the voting part yet, because this error keeps coming where, anytime more then 4 ids are read, the last entry is reread and and repeated into the text file.

The text file layout is like this:
Daniel 18 G
Ken 52 F
Mark 12 X
Riley 17 X
Ian 22 G
Harry 15 F

And the data is then sorted into two files, labelled "Under 18" and "Over 18".
But if the data happens to have more than 4 entries, the last entry will get repeated, something like this:

Name: Harry
Age: 15

Name:
Age: 15

Here is the code:

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <fstream>
using namespace std;

void Data(char[], char, int);
void Over_18(char[], char, int);
void Under_18(char[], char, int);

int main()
{
	char Name[100] = {}, Vote = {};
	int Age = {};

	Data(Name, Vote, Age);
}

void Data(char Name[], char Vote, int Age)
{
	ifstream Main;
	Main.open("Data.txt", ios::app);

	while (!Main.eof())
	{
		Main >> Name;

		if (Name == "0")
		{
			break;
		}

		Main >> Age;
		Main >> Vote;

		if (Age < 18)
		{
			Under_18(Name, Vote, Age);
		}
		else if (Age >= 18)
		{
			Over_18(Name, Vote, Age);
		}

		for (int i = 0; i < 100; i++)
		{
			Name[i] = -52;
		}
	}
}

void Under_18(char Name[], char Vote, int Age)
{
	ofstream f18;
	f18.open("Under 18.txt", ios::app);

	f18 << "Name: " << Name << endl;
	f18 << "Age: " << Age << endl << endl;

	f18.close();
}

void Over_18(char Name[], char Vote, int Age)
{
	ofstream f19;
	f19.open("Over 18.txt", ios::app);

	f19 << "Name: " << Name << endl;
	f19 << "Age: " << Age << endl;
	f19 << "Vote: " << Vote << endl << endl;

	f19.close();
}


I would like to add that yes, I am aware vectors could help, as some people in my previous questions pointed out. But I haven't looked into it yet and I want to find a solution to this problem with just fstream.
Using .eof() as a loop condition is almost always wrong. The end-of-file flag is not set until you've tried to read past the end. This is why you end up going through the loop one extra time.
Last edited on
Do you mean something like this:

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
#include <iostream>
#include <fstream>
#include <string>

void Data();
void Over_18(const std::string& ,int, char);
void Under_18(const std::string&, int);

int main() {
	Data();
}

void Data() {
	std::ifstream Main("Data.txt");

	if (Main) {
		int Age {};
		char Vote {};
		std::string Name;

		while (Main >> Name >> Age >> Vote)
			Age < 18 ? Under_18(Name, Age) : Over_18(Name, Age, Vote);
	} else
		std::cout << "Cannot open file\n";
}

void Under_18(const std::string& Name, int Age) {
	std::ofstream f18("Under 18.txt", std::ios::app);

	f18 << "Name: " << Name << '\n';
	f18 << "Age: " << Age << "\n\n";
}

void Over_18(const std::string& Name, int Age, char Vote) {
	std::ofstream f19("Over 18.txt", std::ios::app);

	f19 << "Name: " << Name << '\n';
	f19 << "Age: " << Age << '\n';
	f19 << "Vote: " << Vote << "\n\n";
}


This will read the input file and create the 2 output files as required.
Topic archived. No new replies allowed.