Part of my program is not displaying correctly

Assignment BLUF:

Read numbers from a file; display an "*" for every 1000 people.

This is part of an assignment for my beginners C++ class. I am getting accurate results from the program, however it is not displaying the "*" in a bar, it is displaying them vertically. I have tried different placements of brackets and endl;'s but none of my modifications have given me the correct output yet. Any suggestions would be greatly appreciated.

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

using namespace std;

// Constants for exercise:
// Town of Prairieville
// 20 year intervals from 1900-2000
// Display * for every 1000 people

int main()
{
	ifstream inputfile;
	int year;
	int population;
	int a;
	inputfile.open("C:\\data\\people.txt");


	if (inputfile.fail()) //Testing for file open error
	{
		cout << "Error opening file.\n";
	}
	else

		cout << "PRAIRIEVILLE POPULATION GROWTH" << "\n"; //Printing Header info
		cout << "(each * represents 1000 people)" << "\n";
		cout << "-------------------------------\n";

			for (year = 1900; year <= 2000; year += 20) // Establishing Year range
			{
				cout << year << " ";

				inputfile >> population;
					for (a = 0; a < population; a += 1000) // Establishing asterics
						cout << "*";
			}
		
	

	inputfile.close();

	system("pause"); //Used to keep window open

	return 0;
}
1) You forgot braces aroung else statement.
2) Almost everything is all right, you just need to output endline after inner loop before outer ends: http://coliru.stacked-crooked.com/a/3a394df2c5eba3ea
That was it, thanks for the pro tip!!
Topic archived. No new replies allowed.