pop bar chart help

Hi, new to the forum and programming, I've seen a few threads on this and I've taken tidbits here and there to get as far as I have.. but I'm still missing something here. The problem from the textbook states,

18. Population Bar Chart
Write a program that produces a bar chart showing the population growth of Prairieville, a small town in the Midwest, at 20-year intervals during the past 100 years. The program should read in the population figures (rounded to the nearest 1000 people) for 1900, 1920, 1940, 1960, 1980, and 2000 from a file. For each year display the date and a bar consisting of one asterick for each 1000 people. The data can be found in the People.txt file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
        ifstream inputFile;	//file stream object
	int number;		//declare variable
	inputFile.open("People.txt");	//open input file
	cout << "PRAIRIEVILLE POPULATION GROWTH\n" << "(each * represents 1000 people)\n";	//title description
	for (int y = 1900; y <= 2000; y += 20)
	{
		cout << y << ' ';      //display years
		inputFile >> number;
		for (int i = 0; i < number; i += 1000)	//increments of 1000
		{
			cout << '*';	//display astericks
		}
		cout << endl;
	}
	inputFile.close();		//close file
	system("pause");
	return 0;
}


my People.txt file is:
1900 2230
1920 4532
1940 5783
1960 6466
1980 8222
2000 10348

but, my output is:

PRAIRIEVILLE POPULATION GROWTH
(each * represents 1000 people)
1900 ***************************************************************************
*************************************
1920 ***************************************************************************
*************************************
1940 ***************************************************************************
*************************************
1960 ***************************************************************************
*************************************
1980 ***************************************************************************
*************************************
2000 ***************************************************************************
*************************************
Press any key to continue . . .

I've been trying to figure this out for a few days, it's taken me a while to get this far so I didn't want to start completely over again, but I'm not sure where I'm going wrong. If it's not reading from the file proper, if I need to introduce the populations manually, or if I'm displaying the astericks wrong in the code. Any help would be greatly appreciated!
The first problem is that you have two numbers per record (the year and the population).
However, at line 14, you're only reading one number. Your're going to read six numbers in your for loop. Those numbers will be: 1900 2230 1920 4532 1940 5783.

The second problem is that your loop is hard coded for specific years, even though the years are provided in the data file. If you wanted to run your program with a different set of years, you would have to change your program. A better method is the following:
11
12
13
14
15
16
17
18
19
    int asterisks;

    while (inputfile >> y >> number) 
    {   cout << y << ' ';      //display years
         asterisks = number / 1000;
	 for (int i=0; i<asterisks; i++)	
	    cout << '*';	//display asterisks 
	cout << endl;
    }

Also it appears that your file is probably not opening correctly, you really need to check to make sure it opens correctly.
Topic archived. No new replies allowed.