Bar Graph Manipulation assistance

Here is my code for population growth. The output is nearly perfect, I just need the '*' to be beside the date and then endline to the next year. As well as, display the number that is in the infile for the respective population for that year. IE: "1850: *** 2863"

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

using namespace std;

int main()
{
    ofstream out("population.out");
    ifstream inf("population.in");
    if(!inf)
        cout << "ERROR";

    float year, pop, asterisks;

    out << "  Year \t\t\t   Population\n"
        << " (one * = 1,000)\n"
        << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

    while(inf >> year >> pop)
    {
        out << year << ":" << ' ';
        asterisks = pop / 1000;
        out<< endl;
        for(int i=0; i<asterisks; i++)
            out << '*';
            out << endl<<endl;

    }
        out << endl;
out.close();
inf.close();
return 0;


The INFILE text is as follows:
1850 2863
1860 3634
1870 4907
1880 4977
1890 7995
1900 8068
1910 7611
1920 8018
1930 11554
1940 13050
1950 16437
1960 72365

My OUTPUT:
Year Population
(one * = 1,000)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1850:
***

1860:
****

1870:
*****

1880:
*****

1890:
********

1900:
*********

1910:
********

1920:
*********

1930:
************

1940:
**************

1950:
*****************

1960:
*************************************************************************

How I would like the OUTPUT to look:
Year Population
(one * = 1,000)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1850:*** 2863

1860:**** 3634

1870:***** 4907

1880:***** 4977

1890:******** 7995

1900:********* 8068

1910:******** 7611

1920:********* 8018

1930:************ 11554

1940:************** 13050

1950:***************** 16437

1960:************************************************************************* 72365

Last edited on
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 <fstream>
#include <sstream>
#include <iomanip>
using namespace std;

int main()
{
//  ofstream out("population.out");
//  ifstream inf("population.in");
    ostream &out = cout;
    stringstream inf( 
"1850 2863"
"1860 3634"
"1870 4907"
"1880 4977"
"1890 7995"
"1900 8068"
"1910 7611"
"1920 8018"
"1930 11554"
"1940 13050"
"1950 16437"
"1960 72365" )

    if(!inf)
        cout << "ERROR";

    int year, pop, asterisks;                      // Whole numbers

    out << "  Year \t\t\t   Population\n"
        << " (one * = 1,000)\n"
        << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

    while(inf >> year >> pop)
    {
        out << year << ":" << ' ';
        asterisks = pop / 1000.0 + 0.5;
//        out<< endl;                              // Don't line feed
        for(int i=0; i<asterisks; i++)
            out << '*';
        out << " " << pop << "\n\n";               // Indent correctly and just write the population!
    }
    out << '\n';
//out.close();                                     // Not needed
//inf.close();                                     // Not needed
//return 0;                                        // Not needed
}


Your population must have had some fun in the 1950s!
Last edited on
Topic archived. No new replies allowed.