Problem with math in code

closed account (z8q4izwU)
Hello, im having problems with my code. Everything works besides it calculating a percent of the total votes. This is what the book says to input
Johnson 5000
Miller 4000
Duffy 6000
Robinson 2500
Ashtony 1800
Every thing works as it should besides calculating a percent it just out puts 0%. If anyone has any thoughts or ideas that would fix this i would appreciate it.

Thanks




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
74
75
#include <cstdlib>
 #include <iostream>
 #include <string>
 #include <iomanip>
 
using namespace std;
 //Define protypes of 2 functions:
 int winnerIndex (int votes[]);
 
int main(int argc, char *argv[])
 {
 
//Declare Variables
 
string name[5];
 int votes[5];
 double totalVotes;
 int i;
 cout << fixed << showpoint;
 cout << setprecision(2);
 

//Perform loop to load the 2 arrays
 for (i = 0; i < 5; i++)
 {
 cout << "Enter Last name of the candidate" << endl;
 cin >> name[i];
 cout << "Enter amount of votes the candidate received" << endl;
 cin >> votes[i];
 }
 


for (i = 0; i < 5; i++)
 {
 totalVotes += votes[i];
 }
 

// Output the heading line
 cout << "\nCandiate" << '\t' << "Votes Received" << '\t' << " % of Total Votes" << endl;
 
// Perform a loop, which uses the contents of the arrays, to output the detail lines
 for (i = 0; i < 5; i++)
 {
 cout << name[i] << "\t\t" << " " << votes[i] << "\t\t" << " " << (votes[i]/totalVotes) * 100 << endl;
 }
 
cout << "\nTotal------" << totalVotes << endl;
 
i = winnerIndex(votes); // function call
 //Output th winner of the election
 cout << "The winner of the election is: " << name[i] << endl;
 
system("PAUSE");
 
}
 
//Function Definition
 int winnerIndex (int votes[])
 {
 int i = 0;
 int max = 0;
 
for (i = 0; i < 5; i++)
 {
 if(votes[i] > votes[max])
 {
 max = i;
 }
 }
 
return max;
 
}
Last edited on
Enter Last name of the candidate
a
Enter amount of votes the candidate received
2
Enter Last name of the candidate
b
Enter amount of votes the candidate received
4
Enter Last name of the candidate
c
Enter amount of votes the candidate received
6
Enter Last name of the candidate
d
Enter amount of votes the candidate received
8
Enter Last name of the candidate
e
Enter amount of votes the candidate received
10

Candiate	Votes Received	 % of Total Votes
a		 2		 6.67
b		 4		 13.33
c		 6		 20.00
d		 8		 26.67
e		 10		 33.33

Total------30.00
The winner of the election is: e


Works fine.
closed account (z8q4izwU)
It must be my computer or something because it still doesnt work for me.
Topic archived. No new replies allowed.