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
|
#include <iostream>
#include <cstdio>
using std::cout;
using std::cin;
const char ERROR = '\0';
int main()
{
int num_reps;
int i; // counter used by for loop
int democrats = 0, republicans = 0, independents = 0;
char party;
float perc_demo = democrats/num_reps*100;
float perc_rep = republicans/num_reps*100;
float perc_ind = independents/num_reps*100;
cout << "\nHow many U.S. Representatives does your state have? ";
cin >> num_reps; // ask user for number of representatives
cout << "Enter the party affiliation for each Representative.\n";
cout << "Enter D for Democrat, R for Republican,\n";
cout << "and I for independents or other parties.\n";
for (i = 1; i <= num_reps; i++)
{
do
{
cout << "Party of representative #" << i << ": ";
cin >> party;
switch(party)
{
case 'D':
case 'd': // if democrat,
democrats++; // increment democrats counter
break;
case 'R':
case 'r': // if republican,
republicans++; // increment republicans counter
break;
case 'I':
case 'i': // if independent or other,
independents++; // increment independents counter
break;
default:
cout << "Invalid entry. Enter D, R, or I.\n";
party = ERROR;
break;
} // end of switch structure
} while (party == ERROR); // loop again if invalid choice is made
} // end of for loop
cout << "\nYour state is represented by " << democrats << " Democrats, "
<< republicans << " Republicans,\nand " << independents
<< " independents and members of other parties.\n\n";
cout <<"The percentage of Democrats in your state is, " << perc_demo<<"%\n";
cout <<"The percentage of Republicans in your state is, " << perc_rep<<"%\n";
cout <<"The percentage of Independents in your state is, "<< perc_ind<<"%\n\n";
return 0;
getchar();
} // end of program
|