Jumping into C++ Chapter 5, problem 7

Goodmorning, afternoon, evening and night ;)

I have a question. I am following the Jumping into C++ course of Alex Allain.

I worked on chapter 5, problem 7.

This 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 three inputs are the possible
answers. The first answer is indicated by 1, the second by 2, the third by 3. The answers are
tallied until a 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.

Do you think that this program can be a solution? If you have better suggestions,please let me know..


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

int main()
{

    int pollAnswer;
    int pollAnswer01=0;
    int pollAnswer02=0;
    int pollAnswer03=0;
    int pollEnteredPeople=0;

    cout << "Choose one of the four options or press 0 to quit: " << endl;

    cout << "1. I am amazing: " << endl;
    cout << "2. I am great: " << endl;
    cout << "3. I am fantastic: " << endl;

    cin >> pollAnswer;

    while(pollAnswer!=0)
    {

        pollEnteredPeople++;


        if(pollAnswer == 1)
        {

            pollAnswer01++;
            cin >> pollAnswer;
        }

        else if(pollAnswer == 2)
        {

            pollAnswer02++;
            cin >> pollAnswer;
        }
        else if(pollAnswer == 3)
        {

            pollAnswer03++;
            cin >> pollAnswer;
        }
        else if (pollAnswer >= 4)
        {
            cout << "You did not enter the right number! Program quitting...\n \n";
break;
        }
    }
    cout << "The number of people entered is: " << pollEnteredPeople << endl;

    cout << "nr 1 has: " << pollAnswer01 << " entries" << endl;
    cout << "nr 2 has: " << pollAnswer02 << " entries" << endl;
    cout << "nr 3 has: " << pollAnswer03 << " entries" << endl;

    cout << "\n \nBar Graph: " << endl;
    cout << "Nr.01: " << setw(pollAnswer01+1) << setfill('*') << '\n' ;
    cout << "Nr.02: " << setw(pollAnswer02+1) << setfill('*') << '\n';
    cout << "Nr.03: " << setw(pollAnswer03+1) << setfill('*') << '\n';


    return 0;

}
The first input to the program is the poll question; the next three inputs are the possible answers

It sounds as though the question, and the possible answers to that question, are supposed to be input by the user, rather than being hardcoded into the program.
Hello LadyoftheCave,

Your program works, but as MikeyBoy has pointed out I believe and agree that yo did not follow the instructions correctly.

Anyhow your while loop could be better written as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	std::cout << "\nEnter poll answer: ";
	std::cin >> pollAnswer;

	while (pollAnswer != 0)
	{
		pollEnteredPeople++;

		if (pollAnswer == 1)
			pollAnswer01++;
		else if (pollAnswer == 2)
			pollAnswer02++;
		else if (pollAnswer == 3)
			pollAnswer03++;
		else if (pollAnswer >= 4)
		{
			std::cout << "You did not enter the right number! Program quitting...\n \n";
			break;
		}

		std::cout << "\nEnter poll answer: ";
		std::cin >> pollAnswer;
	}

The if/else if statements could be replaced with a switch.

In these statements:
1
2
3
4
std::cout << "\n \nBar Graph: " << std::endl;
std::cout << "Nr.01: " << std::setw(pollAnswer01 + 1) << std::setfill('*') << '\n';
std::cout << "Nr.02: " << std::setw(pollAnswer02 + 1) << std::setfill('*') << '\n';
std::cout << "Nr.03: " << std::setw(pollAnswer03 + 1) << std::setfill('*') << '\n';

The "std::setfill" only needs to be done once and it will stay that way until it is changed. Most times it is best to put "std::setfill" back to a space when you are done. In this case the program ends and it is not necessary.


Choose one of the four options or press 0 to quit:
1. I am amazing:
2. I am great:
3. I am fantastic:

Enter poll answer:


So what is the forth option?

Hope that helps,

Andy
Thank you Andy and Mikeyboy,

I will work on it :)
Topic archived. No new replies allowed.