Array not being assigned correct value.
Nov 30, 2015 at 7:32am UTC
My objective in this assignment is to enter votes for each politician based on user input of votes. The part that is currently giving me problems is where I am trying to get the array percentageOfVotes to be assigned the correct value by having the (vote / totalVotes) * 100. Every time I run the program, the value for each percentageOfVotes always comes up as 0. Please explain why and how I can fix.
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
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
//Function Prototypes
void printList();
int getTotalVotes();
int main()
{
// Array declaration
string candidate[5];
candidate[0] = "Washington" ;
candidate[1] = "Franklin" ;
candidate[2] = "Benjamin" ;
candidate[3] = "Lincoln" ;
candidate[4] = "Kennedy" ;
int vote[5];
vote[0] = 0;
vote[1] = 0;
vote[2] = 0;
vote[3] = 0;
vote[4] = 0;
//do while loop - receive votes
char another;
int x = 0;
int totalVotes = 0;
do
{
cout << "Enter votes for " << candidate[x] << ": " ;
cin >> vote[x];
cout << "Another? (y or n) : " ;
cin >> another;
totalVotes = totalVotes + vote[x];
x++;
} while (another == 'Y' || another == 'y' && x < 5);
cout << endl << endl;
// for loop - display output
double percentOfVote[5];
percentOfVote[0] = vote[0] / totalVotes * 100;
percentOfVote[1] = vote[1] / totalVotes * 100;
percentOfVote[2] = vote[2] / totalVotes * 100;
percentOfVote[3] = vote[3] / totalVotes * 100;
percentOfVote[4] = vote[4] / totalVotes * 100;
cout << setw(10) << "Candidate " << setw(7) << "Votes "
<< setw(14) << "% of Total" << endl;
cout << setw(10) << "---------- " << setw(7) << "----- "
<< setw(14) << "---------- " << endl;
for (int y= 0; y < 5; y++)
{
cout << setw(10) << candidate[y] << " " << setw(4) << vote[y]
<< " " << setw(12) << percentOfVote[y] << endl;
}
return 0;
}
Nov 30, 2015 at 7:59am UTC
int vote[5]; // Change it to double
Nov 30, 2015 at 9:54pm UTC
@TarikNeaj
Thank you.
Topic archived. No new replies allowed.