dimanche 12 juin 2016

C++ column chart is reversed


I'm stumped on why my columns are reversed. The actual column graph (asterisks only) needs to be flipped horizontally. (The population in 1900 is 2000 and the population in 2040 is 24000.)

Here is what it currently looks like:

Population column chart

24000  **
23000  **
22000  **    **
21000  **    **
20000  **    **
19000  **    **
18000  **    **    **
17000  **    **    **
16000  **    **    **
15000  **    **    **
14000  **    **    **    **
13000  **    **    **    **
12000  **    **    **    **
11000  **    **    **    **
10000  **    **    **    **
 9000  **    **    **    **    **
 8000  **    **    **    **    **
 7000  **    **    **    **    **
 6000  **    **    **    **    **
 5000  **    **    **    **    **    **
 4000  **    **    **    **    **    **    **
 3000  **    **    **    **    **    **    **
 2000  **    **    **    **    **    **    **    **
 1000  **    **    **    **    **    **    **    **
      1900  1920  1940  1960  1980  2000  2020  2040

And here's the code:

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

int main(){
    int population_size = 8; //Available for changes. 
    int year = 1900;
    int population[population_size];
    bool haveFile = true;

    const string inFileName = "people.txt";
    ifstream inFile("people.txt"); //Defines input file and opens

    cout << "Population column chart" << endl;
    cout << endl;


    if (inFile){
        // cout << "Open of " << inFileName << " was successful.n";
        int count = 0;
        while (!inFile.eof()) {
            inFile >> population[count];
            count++;
        if (inFile.eof()) break;}

        int min = 1000; //Define minimum y-axis amount
        int max = 0; //Initialize max value to 0
        for (int i=0; i<count; i++){ //Find the maximum population 
            if(population[i]> max);
            max = population[i];
        }
         //This block creates the y-axis legend and rows of stars
        for (int num = max; num >= min; num = num - 1000){ 
            cout << setw(5) << num << " ";
            for (int year_counter = 0; year_counter <= count-1; year_counter++)
                if (num <= population[year_counter])
                    cout << setw(3) << "**" << "   ";
            cout << endl;
        }
        //This block creates the x-axis legend
        cout << "    "; 
        for (int k = 1; k <= count; k++){
            cout << setw(6) << year;
            year += 20;
        }
    }
}

The problem is in this area of code:

for (int year_counter = 0; year_counter <= count-1; year_counter++)
            if (num <= population[year_counter])
                cout << setw(3) << "**" << "   ";

population[0] should be a value of 2000, but it is 24000. Is there a problem with my array being somehow reversed?

Thanks in advance


Aucun commentaire:

Enregistrer un commentaire