lundi 29 juin 2015

Having an issue reading from a text file for a Population Bar Chart program


From instructor: "Complete programming problem #18 on page 296 of the textbook. The text file People.txt to use for your program is attached to this assignment. I will use this same text file to test your program after it is submitted so do not make any changes to the text file contents. Submit the .cpp file for your program to this assignment."

Book: "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 1,000 people) for 1900, 1920, 1940, 1960, 1980, and 2000 from a file. For each year it should display a date and a bar consisting of one asterisk for each 1,000 people. The data can be found in the People.txt file.

People.txt: 2000 4000 5000 9000 14000 18000 18000

When written as below, with error checking integrated within my if statements, it doesn't give me any values, but instead immediately skips to the else statement. I have output that now reads from the file but lists 2000 then 1900 - 2000 with the asterisk population values not properly reflecting the population values within the txt file.

#include <iostream> //for cout statement
#include <fstream> //for file access
using namespace std;

int main()
{
int year, population;
//defines filestream object and opens file
ifstream inFile;
inFile.open("People.txt");

//Standard output statements providing framework for chart
cout << "Population Growth of Prairieville\n";
cout << "\n[Each asterisk (*) represents 1,000 people]\n";
cout << "\nYear \t\t Population\n";
cout << "___________________________________________\n";

//if file opens process
if (inFile)
{
    //Reads to "population" variable
    inFile >> population;
    cout << population << endl;

    for (year = 1900; year <= 2000; year += 20)
    {
        cout << year;
        inFile >> population;

        for (int star = 1; star < population; star += 1000)
        {
            cout << "*";
        }

        cout << "\n" << endl;
    }

    //close file
    inFile.close();
}

else
{
    //Display error message
    cout << "Error opening file. Shutting down. \n";
}

system("PAUSE");
return 0;
}

I am trying to sequentially access the information within the text file so that it reads from the text file for all values. I don't know if it has anything to do with the extra 18000 the professor added in the text file but I feel that shouldn't have anything to do with that if I'm giving the program exactly what to look for in my for loop. I have seen this problem elsewhere but everyone seems to give very complicated solutions to what seems to me like it should be a simple answer.

Please note: My apologies for not being able to include a screenshot of my ouput in the console. I also do not intend to keep system "PAUSE"; as I know it's considered "sloppy", but I need it as of right now for eval purposes.

Thank you!!!


Aucun commentaire:

Enregistrer un commentaire