I've been working on an assignment:
Write a program that produces a bar chart of population growth for a small town, at 20 year intervals during the past 100 years. It should read the populations rounded to the nearest 1000 people. for each year it should display the date and bar consisting of one asterisk for each 1000 people.

the data file contains:
2157
4289
5317
7215
10789
12974

This is what I have so far

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ifstream inputFile;
	int pop;
	int count = 0;

	inputFile.open("c://people.dat");

	if (!inputFile)
	{
		cout << "Error opening file.\n";
	}
	else
	{
		cout << "\t\t\tPrarieville Population Growth\n";
		while (count <= 6)
		{
			inputFile >> pop;
			count++;
		}

		for (int years = 1900; years <= 2000; years = years + 20)
	{
        cout<<years<<endl;  
	}
				if ( pop >= 1499 || pop <= 2499)
				{
					cout << "**\n";
				}
				else if (pop >= 3499 || pop <= 4499)
				{
					cout << "****\n";
				}
				else if (pop >= 4499 || pop <= 5499)
				{
					cout << "*****\n";
				}
		
				else if (pop >= 6499 || pop <= 7499)
				{
					cout << "*******\n";
				}
		
				else if (pop >= 10499 || pop <= 11499)
				{
					cout << "***********\n";
				}
				else if (pop >= 12499|| pop <= 13499)
				{
					cout << "*************\n";
				}
	}
return 0;
}

But I'm not getting the correct asterisk amounts, and I need the asterisk to be beside the year to show the population. =/ I'm a beginner ...

Recommended Answers

All 2 Replies

With the two loops, while and for, you are essentially discarding pop before you get to the for loop. Your if-tree uses || where you mean && .

cout << "\t\t\tPrarieville Population Growth\n";
        for ( int years = 1900; years <= 2000; years = years + 20 )
        {
            inputFile >> pop;
            cout<<years;  
            if ( pop >= 1499 && pop <= 2499 )
            {
                cout << "**\n";
            }
            /* ... */

Thank you so much. I can't believe I didn't realize I was using the OR not the AND. Question Solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.