Hi, I'm reading "Jumping into C++" and I am having trouble completing a practice problem at the end of the chapter on loops.
Here is the problem:
"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 have an idea of how to do it but it's still incorrect.
Here is what I was able to do:
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 <<"\n\n1 - Rugby\n2 - Soccer\n3 - Hockey";
cout <<"\n\n\nEnter your favourite (0 when finished): ";
cin >>sport;
if (sport == 1)
{
rugby++;
count1++;
}
if (sport == 2)
{
soccer++;
count2++;
}
if (sport == 3)
{
hockey++;
count3++;
}
if (sport != 1 && sport != 2 && sport !=3 && sport !=0)
{
cout <<"\n\n\nInvalid sport\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";
}
This program is incorrect because it does not display it as a proper graph (no y axis) and i have no idea how to scale it to fit the screen no matter how many results are entered.
Could someone please show me the source code I would need to complete this program or explain how to do it in detail? Please keep in mind i am very new to C++ and i only know very simple stuff
Thanks so much