While Loop to read out file information

Hi guys! So I'm working on an assignment for class where we have to use a while loop to read data out of a file and display it in our program. In the program we are only given certain information and have to figure two columns out on our own using equations. My code when run only displays the first line for the document, leaving out the country name completely and just displays the same line over and over again without ever stopping. I would love if someone could help because at this point I'm not even confident I'm anywhere near this code being correct! Thank you SO much. Your help would be a life (and a grade) saver!!!

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
  int main()

{
	ifstream inputFile;
	ofstream outputFile;
	float popAverage, interAverage, userAverage, total, penetration, growthPercent,
		pentAverage, growAverage, sumPop, sumInt, sumUser, sumPent, sumGrowth;
	int population, internet, usersCurrent, area;

	//Open file.
	inputFile.open("pgm4.txt");
	outputFile.open("pgm4rpt.txt");
	if (!inputFile)
	{
		//Display error messeage.
		cout << "Error opening your file.\n";
	}

	cout << "World Internet Usage Report \n";
	cout << " M T, CS2010, Tuesday Thursday @6pm \n ";
	cout << "\n";
	cout << "Area	Population	Internet(2000) Users Current	Penetration (% Pop)		Growth% ";
	cout << "___________________________________________________________________________________";

	// read in
	while (!inputFile.eof())
	{
		inputFile >> area >> population >> internet >> usersCurrent;
		cout << area << "	" << population << "	" << internet << "	" << usersCurrent << "	" << penetration << "	" << growthPercent << endl;
		area++;
		if (area == 1)
		{
		penetration = usersCurrent / population;
		growthPercent = (usersCurrent - internet) / internet;

		}
		else if (area == 2)
		{
		penetration = usersCurrent / population;
		growthPercent = (usersCurrent - internet) / internet;
		}
		else if (area == 3)
		{
		penetration = usersCurrent / population;
		growthPercent = (usersCurrent - internet) / internet;
		}
		else if (area == 4)
		{
		penetration = usersCurrent / population;
		growthPercent = (usersCurrent - internet) / internet;
		}
		else if (area == 5)
		{
		penetration = usersCurrent / population;
		growthPercent = (usersCurrent - internet) / internet;
		}
		else if (area == 6)
		{
		penetration = usersCurrent / population;
		growthPercent = (usersCurrent - internet) / internet;
		}
		else if (area == 7)
		{
		penetration = usersCurrent / population;
		growthPercent = (usersCurrent - internet) / internet;
		}


		sumPop += population;
		sumInt += internet;
		sumUser += usersCurrent;
		sumPent += penetration;
		sumGrowth += growthPercent;
		}

		//Calculate average number of each category.

		popAverage = sumPop / area;
		interAverage = sumInt / internet;
		userAverage = sumUser / usersCurrent;
		pentAverage = sumPent / penetration;
		growAverage = sumGrowth / growthPercent;

		cout << "____________________________________________________________________________" << endl;
		cout << "Total/Avg %: " << popAverage << "	" << interAverage << "	" << userAverage << "	" << pentAverage << "	" << growAverage << "	" << endl;

		//Output to file
		outputFile << ("pgm4rpt.txt");

		//Close the file.
		inputFile.close();
		outputFile.close();

		cout << endl;


	cout << endl;
	system("pause");
	return 0;
Is sounds like your input stream is getting hosed. Always make sure that anything from the keyboard didn't kill your program:

1
2
3
4
5
6
7
8

inputFile >> area >> population >> internet >> usersCurrent;

if( inputFile.good() )

//   the rest of your program



I hope this helps.
I would actually prefer to check for file readability before I do any sort of operation on the file. It's like if you go shopping on Amazon for books. What's the first thing you see, besides title and ISBN and all that? It's certainly not the contents of the book...it's the condition the book is in, given by the seller. So what I usually do is:

1
2
3
4
5
6
7
8
if( !inputFile.fail() )
{
    while( !inputFile.eof() )
    {
        // do stuff
    }
}
else // maybe catch an error here if it fails to open for whatever reason 
Last edited on
Can't figure out what exactly went wrong in the code. Maybe you could provide a sample input file?
Hi,

One shouldn't loop on eof, loop on the filestream instead.

1
2
3
while (inputFile) {

}


The integers types are doing integer division which is causing problems, even though they are implicitly cast to float in the assignment.

With braces, line them up in the same column as the keyword that starts the compound statement. I had trouble seeing where the while loop ended.

Line 88 I don't see an overloaded << anywhere, how were you intending that to work?

Good Luck !!
Topic archived. No new replies allowed.