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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
usingnamespace 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.
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.
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.