Hi, I'm reading a book on C++ and one of the practice problems after the intro to loops reads:
"write a program that provides the option of tallying up the results of a poll with 3 possible values. The first input to the program is the poll question; the next 3 inputs are the possible answers. The first answer is indicated by 1, the second by 2 and the third by 3. The answers are tallied until 0 is entered. The program should then show the results of the poll--try making a bar graph that shows the results properly scaled to fit on your screen no matter how many results were entered."
I can create a program where the user types in a few numbers and bar graph which looks like this is created:
Rugby(6): ||||||
Soccer(3): |||
Hockey(4): ||||
Although this could maybe pass as a bar graph, it is not scaled to fit the screen no matter how many results are entered and I think it should look more like this:
6 |
5 |
4 | |
3 | | |
2 | | |
1 | | |
Rugby Soccer Hockey
I would really appreciate it if someone could tell me how I could properly complete the problem
If it helps here is the code which I used to create the program which creates a bar graph which is not scaled to fit the screen:
Code:PS I promise this is not homework, my school does not provide an option to learn C++Code:#include <iostream> #include <string> using namespace std; int main () { int count1=0; int count2=0; int count3=0; int sport=0; int rugby=0; int soccer=0; int hockey=0; do { cout <<"1 - Rugby\n2 - Soccer\n3 - Hockey"; cout <<"\n\n\nEnter your favourite (0 when finished): "; cin >>sport; cout <<"\n\n"; if (sport == 1) { rugby++; count1++; } else if (sport == 2) { soccer++; count2++; } else if (sport == 3) { hockey++; count3++; } else { cout <<"Invalid sport\n\n\n"; } } while (sport != 0); cout <<"\n\n\n\n\n\n\n\n\n\n\n\nRugby: "; while (rugby > 0) { cout <<"|"; rugby--; } cout <<"(" <<count1 <<")"; cout <<"\nSoccer: "; while (soccer > 0) { cout <<"|"; soccer--; } cout <<"(" <<count2 <<")"; cout <<"\nHockey: "; while (hockey > 0) { cout <<"|"; hockey--; } cout <<"(" <<count3 <<")"; cout <<"\n\n\n\n\n\n\n\n"; }
Thanks a lot