### 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````#include #include #include #include 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.