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.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
usingnamespace 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;
}