Accumulate data with switch Statement

Can i know how to i accumulate data.
**REQUIRE TO USE SWITCH STATEMENT
use 0 to quit the program and show the vote accumulated.

sample output:

Out of the following 5 free TV channels, which one is your favourite
1. TV1
2. TV2
3. TV3
7. MTV7
8. 8TV
Enter <1, 2, 3, 7 or 8): 3

Out of the following 5 free TV channels, which one is your favourite
1. TV1
2. TV2
3. TV3
7. MTV7
8. 8TV
Enter <1, 2, 3, 7 or 8): 4
Channel 4 doest no exist.
Please try again.

Out of the following 5 free TV channels, which one is your favourite
1. TV1
2. TV2
3. TV3
7. MTV7
8. 8TV
Enter <1, 2, 3, 7 or 8): 2

Out of the following 5 free TV channels, which one is your favourite
1. TV1
2. TV2
3. TV3
7. MTV7
8. 8TV
Enter <1, 2, 3, 7 or 8): 0

TV channel        Vote            Popluarity (%)
TV 1                0                 0.00
TV 2                1                 7.14
TV 3                2                14.29
NTV7                7                50.00
8TV                 4                28.57



This my program:

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
  #include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

int main()
{
	int tv1=0,tv2=0,tv3=0,mtv7=0,tv8=0;
	int choice,total;
	double popularity;
	

	do
	{
	cout<<"Out of the following 5 free TV channels, which one is your favourite"<<endl;
	cout<<"1. TV1"<<endl;
	cout<<"2. TV2"<<endl;
	cout<<"3. TV3"<<endl;
	cout<<"7. MTV7"<<endl;
	cout<<"8. 8TV"<<endl;
	cout<<"Enter <1, 2, 3, 7 or 8): ";

	while(cin>>choice)
	{
		switch(choice)
		{
		case 1:tv1++;
			break;
		case 2:tv2++;
			break;
		case 3:tv3++;
			break;
		case 7:mtv7++;
			break;
		case 8:tv8++;
			break;
		default:
		cout<<"Channel "<< choice <<" does not exist "<<endl;
	        cout<<"Please try again."<<endl;
                cout<<endl;
	    cout<<"Out of the following 5 free TV channels, which one is your favourite"<<endl;
	    cout<<"1. TV1"<<endl;
	    cout<<"2. TV2"<<endl;
	    cout<<"3. TV3"<<endl;
	    cout<<"7. MTV7"<<endl;
	    cout<<"8. 8TV"<<endl;
	    cout<<"Enter <1, 2, 3, 7 or 8): ";
		break;
	        }
		
	}
	}while(choice!=0);
	
	system("Pause");
	return 0;
}	


Im new to C++ programming. I'm stuck because i dont know how to use what code to accumulate the data. Anyone can give me some advice or guidance on how to do it ? For the popularity
i know that is TOTAL VOTE divide by each channel.
Last edited on
Ignore the switch for a moment.

You do have essentially this in your code:
1
2
3
4
5
int tv1 = 0; // set initial value
while ( /* user gives more data */ ) {
  ++tv1; // add to value, aka increment variable, aka accumulate to variable
}
// What is the value of tv1 now? 


(Your actual code has more than one variable, but the logic is same.)


Lets assume that you do figure out the total too. The total is logically an integer.

If you do tv1 / total -- an integer division, the result is an integer and the fraction is discarded; i.e. result is 0.

Therefore, you have to force floating point math like this:
static_cast<double>( tv1 ) / total
Once your outer do-while loop has finished (i.e. around line 54), tv1 will contain the NUMBER of votes for channel TV1, tv2 the number of votes for channel TV2 etc. So:

(1) Create a new int variable (say totalVotes) that will add up the total number of votes, by tv1 + tv2 + etc.

(2) Create new double variables (NOT ints) to get the FRACTIONAL number of votes for each channel; say fracTv1 = tv1 / (double) totalVotes. (The (double) will ensure that you don't do integer division, which is likely to lead to rounding down (to 0).

(3) Multiply each fraction by 100.0 to get percentages.

You can obviously combine (2) and (3) if you prefer.

(4) Output a header line and then, for each channel the relevant statistics. Check out the tutorial on input/output on this website if you want things formatted / set out in tidy columns.

If you don't want to have lots of extra variables, you can do all the analysis and output (2),(3),(4) channel-by-channel and reuse the same variable to hold fractions and/or percentages.

You will learn most if you try to write some code yourself first.
Last edited on
@keskiverto Thanks for the reply. If possible i want to use switch statement.
@lastchance Thanks for the reply. I will try it out.

Thanks a lot.
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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;

int main()
{
	int tv1=0,tv2=0,tv3=0,mtv7=0,tv8=0;
	int choice;
	int totalvotes;
	double fracTV1=0,fracTV2=0,
		fracTV3=0,fracMTV7=0,frac8TV=0;

	do
	{
	cout<<"Out of the following 5 free TV channels, which one is your favourite"<<endl;
	cout<<"1. TV1"<<endl;
	cout<<"2. TV2"<<endl;
	cout<<"3. TV3"<<endl;
	cout<<"7. MTV7"<<endl;
	cout<<"8. 8TV"<<endl;
	cout<<"Enter <1, 2, 3, 7 or 8): ";

	while(cin>>choice)
	{
		switch(choice)
		{
		case 1:tv1++;
			break;
		case 2:tv2++;
			break;
		case 3:tv3++;
			break;
		case 7:mtv7++;
			break;
		case 8:tv8++;
			break;
		default:
		cout<<"Channel "<< choice <<" does not exist "<<endl;
	    cout<<"Please try again."<<endl;
		cout<<endl;
	    cout<<"Out of the following 5 free TV channels, which one is your favourite"<<endl;
	    cout<<"1. TV1"<<endl;
	    cout<<"2. TV2"<<endl;
	    cout<<"3. TV3"<<endl;
	    cout<<"7. MTV7"<<endl;
	    cout<<"8. 8TV"<<endl;
	    cout<<"Enter <1, 2, 3, 7 or 8): ";
		break;
	}	
	}
	}while(choice!=0);
	
	totalvotes= tv1 + tv2 + tv3 + mtv7 + tv8;
	fracTV1= (tv1/(double)totalvotes)/100;
	fracTV2= (tv2/(double)totalvotes)/100;
	fracTV3= (tv3/(double)totalvotes)/100;
	fracMTV7= (mtv7/(double)totalvotes)/100;
	frac8TV= (tv8/(double)totalvotes)/100;
	
	cout<<left;
	cout<<setw(10)<<"TV Channel"<<setw(10)<<"Votes"<<setw(10)<<"Popularity(%)"<<endl;
	cout<<setw(10)<<"TV1"<<setw(10)<<tv1<<setw(10)<<fracTV1<<endl;
	cout<<setw(10)<<"TV2"<<setw(10)<<tv2<<setw(10)<<fracTV2<<endl;
	cout<<setw(10)<<"TV3"<<setw(10)<<tv3<<setw(10)<<fracTV3<<endl;
	cout<<setw(10)<<"MTV7"<<setw(10)<<mtv7<<setw(10)<<fracMTV7<<endl;
	cout<<setw(10)<<"8TV"<<setw(10)<<tv8<<setw(10)<<frac8TV<<endl;

	system("Pause");
	return 0;
}	


This is the latest code i updated. there is error there.
i think is the error
while(cin>>choice)
{
switch(choice)
{
}

Last edited on
Your indentation is not as helpful as it could be. This is what you have:
1
2
3
4
5
6
7
8
9
10
11
do {
  // show menu
  while ( std::cin >> choice ) {
    switch ( choice ) {
      // other cases
      default:
      // show error
      // show menu
    }
  }
} while ( 0 != choice );

You do have two loops.

The inner continues even if user enters 0. The inner stops only if input fails.

If the input fails, then what is stored in the 'choice'? Will that end the outer loop? If not, the loops are infinite. (If input fails, then the stream has fail-bit and all future input auto-fails, unless that error is cleared first.)

You do show the menu only when input was a wrong number. Perhaps you should show the menu before each input?

Two loops are not compulsory. One can use logical and in a condition:
1
2
3
4
5
6
7
8
9
// show menu
while ( std::cin >> choice and 0 != choice ) {
  switch ( choice ) {
    // other cases
    default:
    // show error
  }
  // show menu
}

@keskiverto Thank you for the reply and the mistake you pointed out for me. I get what you mean about the mistake you pointed out. I fixed the code and its works ! Thanks alot.
Topic archived. No new replies allowed.