How to make a bar graph?

I wrote a program that allows the user to generate their own poll and tally up the results of the input. At the end, I want the results to be displayed as a horizontal bar graph. Here is what I have so far:

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  {

    //All the variables and strings

    string poll;
    string a1;
    string a2;
    string a3;

    int a=5;
    int x=0;
    int y=0;
    int z=0;

    int b;
    int c;
    int d;

    //Making the poll

    cout <<"Welcome to your very own poll generator. Please start by entering a question.\n";
    getline(cin, poll);

    cout <<"Excellent. Now please enter three possible choices. \n";
    cout <<"Choice #1: ";
    getline(cin,a1, '\n');

    cout <<"And now your second choice. \n";
    cout <<"Choice #2: ";
    getline(cin,a2, '\n');

    cout <<"And now your third choice. \n";
    cout <<"Choice #3: ";
    getline(cin,a3, '\n');

    //Tallying the poll

    cout <<"Here is your poll. Start the tally by selecting an answer. \n";

    while (a!=0)
    {
        cout << poll << '\n' << "1. "<< a1 << '\n' << "2. " << a2 << '\n' << "3. " << a3 << '\n' << "(Enter 0 to stop the poll.)" << '\n';
        cin >> a;

        if (a==1)
        {
            x++;
            cout <<"Thank you for your time. Next person please. \n";
            continue;
        }

        else if (a==2)
        {
            y++;
            cout <<"Thank you for your time. Next person please. \n";
            continue;
        }

        else if (a==3)
        {
            z++;
            cout <<"Thank you for your time. Next person please. \n";
            continue;
        }

        /*Results of the poll. I tried to take a crack at it but I don't know   how to repeat the asterisk so that they go horizontally.*/

        else if (a==0)
        {
            cout <<"Here are the results of your poll: \n";
            cout <<"1 * = 1 vote. \n";

            cout << a1 << '\n';
            for (b=0;b!=x;b++)
            {
                cout << "* \n";
            }

            cout << a2 << '\n';
            for (c=0;c!=y;c++)
            {
                cout << "* \n";
            }

            cout << a3 << '\n';
            for (d=0;d!=z;d++)
            {
                cout << "* \n";
            }

        }

        else
        {
            cout <<"I'm sorry, that was not a viable answer. Please try again. \n";
            continue;
        }

    }



}
bump
closed account (D80DSL3A)
Omit the \n after each *.
Does this work better for lines 73-89?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout << a1 << '\n';
            for (b=0;b!=x;b++)
            {
                cout << '*';
            }

            cout << '\n' << a2 << '\n';
            for (c=0;c!=y;c++)
            {
                cout << '*';
            }

            cout << '\n' << a3 << '\n';
            for (d=0;d!=z;d++)
            {
                cout << '*';
            }

Yes, perfect! Thank you, I can't believe it was that easy
Last edited on
Topic archived. No new replies allowed.