Need assistance with reading from .txt file.

Pages: 12
Okay so for my computer science class were supposed to be writing a program that reads in information from a text file, okay so that's the easy part but displaying it is becoming the challenging part. Here's an example of what it's supposed to look like....
1
2
3
4
5
6
7
8
9
                        Americas Internet Usage Report
			Your name, CS2010, Class time
 
                             Internet Users   Percent Growth
Area			  Population	      2000       2010	   (% Pop)	2000-2010
NAmerica      344124450     181971633    266224500 146.3
...            ...	        ...	        ...       ...	     ...
 ...            ...	        ...	        ...       ...	     ...

------------------------------------------------------------------
Total/     xxxxxxxxxx   xxxxxxxxx   xxxxxxxxxx   
		
Area with most Internet users (2000):    xxxxxxxxxxxxx 
                        Number Users:    xxxxxxxxxx


Here's my code, at the moment it's very rough as I'm trying to figure this out, this is our first time reading in data from a file and out examples aren't very good. My code probably has a lot of problems but it does run but everything is displayed in correctly and some numbers are missing.


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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
	ifstream inFile;
	string area;
	int population, internetone, ineternettwo;
	double percentgrowth;

	inFile.open("pgm5PB.txt");

		if (!inFile)
		{
			cout << " Error: File could not be opened" << endl;
			return 1;
		}

		
		cout << setw(19) << "Americas Internet Usage Report" << endl;
		cout << setw(5) <<"Parker, CS2010, 2:30PM" <<endl;
		cout << "Area" << setw(20) << "Population" << setw(15) << "2000" << setw(15) << "2010" << setw(20) << "(% Pop)   2000-2010" << endl;
 		while (inFile >> area)
		{
			inFile >> area >> population >> internetone >> percent growth;
		cout << left << setw(15) << percentgrowth;
		}


	inFile.close();
		


	return 0;
}


This is the text file,
NAmerica
344124450
266224500
146.3
SAmerica
396626130
156609436
995.8
CAmerica
154298120
38433400
1094.5
Caribbean
41632722
9647000
1624.5


Okay I know I've included a lot of information here but if anyone has any suggestions or any information at all that can help me, please let me know. I'm kind of stuck on this problem and could really use some advice, my past experience here on the forum was very helpful.

Thanks
Last edited on
You are reading in the Area twice:
1
2
3
4
5
6
 		while (inFile >> area) // once here
		{
			// and again here !
			inFile >> area >> population >> internetone >> percent growth;
		cout << left << setw(15) << percentgrowth;
		}

I would be tempted to go:
1
2
3
4
 		while (inFile >> area >> population >> users >> growth)
		{
			//... output them here
		}

Thanks that was a good suggestion and it worked for me. The problem now it that the internet usage in 2000 is not in the text file so we need to calculate it in the program reading in the users in 2010 and percent of growth between 2000 and 2010. Since I'm not reading in the values for the amount of users in 2000, how would I calculate and integrate it into my output.
That depends what the figures that you do have in the text file mean. Can you give exact definitions?
That depends what the figures that you do have in the text file mean. Can you give exact definitions?


Yeah the first line is the area, those are easy to tell apart because they're the only words. After the words is the population of the area, the second number is the amount of internet users in the year 2010 and the third is the percent increase since the year 2000.


NAmerica - Area
344124450 - Area Population
266224500 - Internet Users 2010
146.3 - % increase from 2000

Okay. This is where knowing a little algebra can take you a long way in programming. My algebra is a little rusty but by my reckoning it should be something like this:
1
2
3
4
5
usage2010 = usage2000 + usage2000 x growth / 100

.: usage2010 = usage2000 (1 + growth / 100)

.: usage2000 = usage2010 / (1 + growth / 100)

So our formula for getting the number of users in 2000 should be:
 
usage2000 = usage2010 / (1 + growth / 100)

Okay so here's what I've got
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
	ifstream inFile;
	string area;
	int population, internetone, internettwo;
	double percentgrowth;

	inFile.open("pgm5PB.txt");

		if (!inFile)
		{
			cout << " Error: File could not be opened" << endl;
			return 1;
		}

		internettwo = internetone / (1 + percentgrowth / 100);

		cout << setw(19) << "Americas Internet Usage Report" << endl;
		cout << setw(5) <<"Parker, CS2010, 2:30PM" <<endl;
		cout << "Area" << setw(20) << "Population" << setw(15) << "2000" << setw(15) << "2010" << setw(20) << "(% Pop)   2000-2010" << endl;
 		while (inFile >> area >> population >> internetone >> percentgrowth)
		{
			cout << area << setw(15) << population << setw(15) << internettwo << setw(15) << internetone << setw(15) << percentgrowth << endl;
		}


	inFile.close();
		


	return 0;


And the output is...
Americas Internet Usage Report
Parker Bushey, CS2010, 2:30PM
Area          Population           2000           2010 (% Pop)   2000-2010
NAmerica      344124450        5932976      266224500          146.3
SAmerica      396626130        5932976      156609436          995.8
CAmerica      154298120        5932976       38433400         1094.5
Caribbean       41632722        5932976        9647000         1624.5
Press any key to continue . . .


I'm having trouble calculation the amount of users in the year 2000, it come up with the same answer for all four areas and I understand why it does that but I'm not sure how to correct it.
The problem is that you calculate internettwo only once. Also you calculate it before you have even read in the values that you need to put into the equation.

You need to do that calculation *after* you read in the values and you have to do it *every time* you read a new set of values.
The problem is that you calculate internettwo only once. Also you calculate it before you have even read in the values that you need to put into the equation.

You need to do that calculation *after* you read in the values and you have to do it *every time* you read a new set of values.


Brilliant.
How would I output the area with the most amount of users in 2000? I've already come up with the totals but I can't figure out out to cout the area with the total number of users in 2000.
Well you could create a variable outside the loop and set it to zero. Then in the loop check each time if the number of users is greater than the number in your variable. If it is then set your variable to that number.

After the loop your variable should contain the highest value.
Well you could create a variable outside the loop and set it to zero. Then in the loop check each time if the number of users is greater than the number in your variable. If it is then set your variable to that number.

After the loop your variable should contain the highest value.


I'll give that a try tonight but all the values for users in 2000 are greater than 0.
That's why you set the initial value to 0. You could also initialize it with the first value and then compare to the others, but that would make the first execution of the loop as special case.
Galik and filipe, that worked. I used an if statement inside the while statement and it worked flawlessly.

One final problem though, I need to output the country with the highest amount of internet users in 2000 as well which and I'm not even sure where to start on this one.

Thanks guys.
Then I would declare a second variable of type std::string along side the one keeping track of the highest value. Every time you set a new highest value, set the string variable to the area. Remeber you read in the area at the same time you read in the other values. So when you find a new highest population to record, you can set your string variable to the area that was read in at the same time.
Last edited on
Well once more you came through, thanks.
Okay well it appears that I have one final problem. I had everything working well (at least I though I did) and I went to add some setw's and somehow my calculations are appearing to be wrong now. I think it has something to do with my numbers not being lined up correctly but not everything is the same length so I'm not sure how I should be lining things up.

Here's what I thought was my final code but it seems like I've still go some work to do although I can't figure out what's wrong.
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
	ifstream inFile;
	string area, highestarea; //Area, highest area for internet users in 2000
	int population, internet2010, internet2000, totalpop, totalint2, totalint1, high2000 = 0; //Population, internet 2010, internet 2000, total population, total internet 2010,
	double percentgrowth; //Percent growth between 2000 and 2010                             //total internet 2000, highest amount of people in 2000
	                                                   
//Open the .txt file
	inFile.open("pgm5PB.txt");

		if (!inFile)
		{
			cout << " Error: File could not be opened" << endl;
			return 1;
		}

		

		cout << setw(55) << "Americas Internet Usage Report" << endl;
		cout << setw(54) << "Parker, CS2010, 2:30PM" << endl;
		cout << setw(74) <<"Internet Users           Percent Growth" << endl;
		cout << "Area" << setw(20) << "Population" << setw(15) << "2000" << setw(15) << "2010" << setw(23) << "(% Pop)   2000-2010" << endl;

		cout << "--------------------------------------------------------------------------------" << endl;
//Output the area the following information for the 4 areas:
//population, internet users in 2000 and 2010, the percent of increase from 2000 to 2010
		
			
 		while (inFile >> area >> population >> internet2010 >> percentgrowth)
		{
//Calculate amount of usuers for the year 2000 and the total population			
			internet2000 = internet2010 / (percentgrowth / 100);
			totalint2 = totalint2 + internet2000;
			totalint2 = totalint2 + internet2000;
//Output the information
			cout << area << setw(15) << population << setw(20) << internet2000 << setw(15) << internet2010 << setw(15) << percentgrowth << endl;
//Calculate highest amount of users in 2000
			
			if (high2000 < internet2000)
			{
				
				highestarea = area;
			}
			totalpop = totalpop + population;
			
			
			
		}

		cout << "--------------------------------------------------------------------------------" << endl;
//Output the totals for population, internet users in 2000 and internet users in 2010
		cout << "Total/     " << totalpop << "   " << totalint2 << "   " << totalint1 << endl;
//Output the area with the most users in the year 2000 and the amount of users in that area in the year 2000
		cout << "Area with most Internet users (2000):    " << highestarea << endl;
		cout << "                        Number Users:    " << high2000 << endl;

//Close the file
	inFile.close();
		


	return 0;
}

This is what I noticed:

In line 11: the only variable you set to zero is high2000. All the others have random values. You need to set each on explicitly.

Line 39 and 40 do the same thing? Why do you need to do this twice?

line 45-49: high2000 never changes value, it stays at zero. So every population is always bigger. You need to set high2000 to the internet2000 if internet2000 is bigger than it, just as you set the highestarea.
That's right, I must of deleted some things on accident. Anyway I can't get my numbers to add up correctly and I think it might be the way they output is lined up.
                         Americas Internet Usage Report
                         Parker Bushey, CS2010, 2:30PM
                                   Internet Users           Percent Growth
Area          Population           2000           2010    (% Pop)   2000-2010
--------------------------------------------------------------------------------

NAmerica      344124450           181971633      266224500          146.3
SAmerica      396626130            15726996      156609436          995.8
CAmerica      154298120             3511502       38433400         1094.5
Caribbean       41632722              593844        9647000         1624.5
--------------------------------------------------------------------------------

Total/     940150102   1943624899   -1315762170
Area with most Internet users (2000):    NAmerica
                        Number Users:    181971633
Press any key to continue . . .


If you notice that the Caribbean line is not lined up with the rest and the numbers with less places aren't line up correctly either. The answers are somewhat close but not correct.

One of the totals is also negative, I've noticed that if I move around the formulas to calculate the totals that some of the totals will be negative and others won't be. My program is a mess now and I tried just to reopen the project I had before but I think Visual Studio autosaves.
Did you fix line 11?

You need to make sure everything is given an initial value:
1
2
3
4
5
6
7
	int population = 0;
	int internet2010 = 0;
	int internet2000 = 0;
	int totalpop = 0;
	int totalint2 = 0;
	int totalint1 = 0;
	int high2000 = 0;

The way you have it above will only set high2000 to zero. That means some of your initial values could be anything.

Also lines 39 and 40 are the same:
1
2
			totalint2 = totalint2 + internet2000;
			totalint2 = totalint2 + internet2000;

Looking at it now I think you probably meant:
1
2
			totalint1 = totalint1 + internet2010;
			totalint2 = totalint2 + internet2000;

The way you line them up should not effect their values. Only the way you calculate them in the program will.
Last edited on
Pages: 12