Still having problems with an extra line being printed with Fstream

Hello, I'm still kind of stuck with that darn eof issue where an extra/extraneous line gets read/printed/etc., and it's messing up my output and my median calculations! I don't think that stringstream is really necessary here, as I am using a struct array. This is my last project for my class, so I would appreciate the help, if you see something bad with my code. I still need to add a menu and make it look nice, but that's not such a big deal.

Thanks, really appreciate the help!!

INPUT DATA FILE (ID Code, Family Income, Family Members):

WHVC 34000.00 5
AAAA 10500.00 8
BURB 23500.00 2
CCCC 15000.00 4
DATA 8000.00 3
EEEE 36000.00 5
FADE 8500.00 4
GATE 25000.00 1
HILO 3000.00 1
JURY 100000.00 5
KNEL 80000 4
LIST 41000.00 3
MEMM 5000.00 2
PQRS 18000.00 2
RELM 27500.00 4
SOLD 22100.00 2

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
 
using namespace std;
 
struct householdData {
 
    string ID_Code;
    double householdIncome;
    int numFamilyMembers;
 
};
 
void inputData(householdData[], ifstream &, int &);
void printData(householdData[], int &i);
void householdsAboveAvg(householdData[], int &i, double &, double &);
void determinePovertyLevel(householdData[], int &i);
void printSortedList(householdData list[], int &i);
void determineMedianIncome(householdData list[], int &i, double &median);
 
void sortList(householdData[], int &i);
 
int main()
{
    double total, average, median;
    householdData list[80];
    int i;
 
 
    ifstream inFile;
    inFile.open("File.txt");
 
    if (!inFile) {
	cout << "\nError: Could not open file.\n";
    }
 
    inputData(list, inFile, i);
    printData(list, i);
    householdsAboveAvg(list, i, total, average);
    determinePovertyLevel(list, i);
    sortList(list, i);
    printSortedList(list, i);
    determineMedianIncome(list, i, median);
 
    return 0;
}
 
void determineMedianIncome(householdData list[], int &i, double &median)
{
 
    median = 0;
 
    sortList(list, i);
 
    if (i % 2 == 0) {
	median = (list[(i / 2) - 1].householdIncome + list[i / 2].householdIncome) / 2;
    }
 
    else
 
	median = list[i / 2].householdIncome;
    cout << "nMedian is " << median << endl;
 
}
 
void printSortedList(householdData list[], int &i)
{
 
    cout << "\nHousehold Incomes Sorted: \n";
 
    for (int k = 0; k < i; k++) {
	cout << list[k].ID_Code << " " << list[k].householdIncome << " " << list[k].numFamilyMembers << endl;
 
}}
 
void sortList(householdData list[], int &i)
{
 
    int swapNumbers = -1;
 
    for (int j = i; j > 0; j--) {
 
	for (int k = 0; k < i - 2; k++) {
 
	    if (list[k].householdIncome > list[k + 1].householdIncome) {
 
		swapNumbers = list[k + 1].householdIncome;
		list[k + 1].householdIncome = list[k].householdIncome;
		list[k].householdIncome = swapNumbers;
 
	    }
	}
    }
}
 
void determinePovertyLevel(householdData list[], int &i)
{
    cout << "\nHouseholds below poverty level: \n";
 
    for (int k = 0; k < i; k++) {
 
	if (list[k].householdIncome <= (8000 + 500 * (list[k].numFamilyMembers - 2))) {
	    cout << setw(7) << list[k].ID_Code << setw(17) << list[k].householdIncome << setw(15) << list[k].numFamilyMembers << endl;
 
	}
    }
}
 
void householdsAboveAvg(householdData list[], int &i, double &total, double &average)
{
 
    total = 0;
    average = 0;
 
    for (int k = 0; k < i; k++) {
	total += list[k].householdIncome;
    }
 
    average = total / i;
 
    cout << "\nThe average household income is: " << average << endl;
    cout << "\nHouseholds above the average income: \n";
 
    for (int k = 0; k < i; k++) {
	if (list[k].householdIncome > average) {
 
	    cout << list[k].ID_Code << "    " << list[k].householdIncome << endl;
 
	}
    }
}
 
void inputData(householdData list[], ifstream & inFile, int &i)
{
 
    i = 0;
 
    while (!inFile.eof()) {
 
	inFile >> list[i].ID_Code >> list[i].householdIncome >> list[i].numFamilyMembers;
	i++;
    }
}
 
void printData(householdData list[], int &i)
{
 
    cout << "\nID Code" << " Household Income " << "Family Members\n";
 
    for (int k = 0; k < i; k++) {
	cout << setw(7) << list[k].ID_Code << setw(17) << list[k].householdIncome << setw(15) << list[k].numFamilyMembers << endl;
}}
Last edited on
instead of :
1
2
3
4
5
6
i = 0;

while (!inFile.eof()) {
    inFile>>list[i].ID_Code>>list[i].householdIncome>>list[i].numFamilyMembers;
    i++;
}


try :
1
2
3
i  = 0;
while( inFile >> list[ i ].ID_Code >> list[ i ].householdIncome >> list[ i ].numFamilyMembers )
    i++;
Try this instead.

1
2
3
4
5
6
7
8
9
10
void inputData(householdData list[], ifstream & inFile, int &i)
{
 
    i = 0;
 
    while (inFile >> list[i].ID_Code >> list[i].householdIncome >> list[i].numFamilyMembers) {////
 
	i++;
    }
}
Awesome - Thanks guys!
Added the menu, I'm pretty much done, except for some small formatting details. Last small little detail is the following - Am just trying to find the percentage of households below poverty level. It seems to be returning 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void determinePovertyLevel(householdData list[], int &i)
{
    int x = 0;
    cout << "\nHouseholds below poverty level: \n";

    for (int k = 0; k < i; k++) {

	if (list[k].householdIncome <= (8000 + 500 * (list[k].numFamilyMembers - 2))) {
	    cout << setw(7) << list[k].ID_Code << setw(17) << list[k].householdIncome << setw(15) << list[k].numFamilyMembers << endl;
         x++;
        }
    }

    cout << "\nPercentage of households below poverty level is " <<  x/i << "%." << endl;
}
Last edited on
Ahhh nevermind, I got it. just needed to make x a double, and multiply that by one hundred.

I think I'm done!
Topic archived. No new replies allowed.