Homework Help Please

I am working on a program to enter and figure out the winner of an election. The only part that seems to not be working is giving the correct winner. Can anyone help me out with what i'm missing please?

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
  #include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int findWinner(int votes[]);
void printResults(string candidates[], int votes[]);
double calculatePercentage(int votes[], int vote);

const int NUMBER_OF_CANDIDATES = 5;

int main()
{
   
    string candidates[NUMBER_OF_CANDIDATES];
    int votes[NUMBER_OF_CANDIDATES];
    
    cout << "Please input the 5 candidates followed by the votes they recieved i.e. Smith 5000: ";
    for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
        cin >> candidates[i] >> votes[i];//Prompts the user to enter five candiates and votes recieved
    }
    printResults(candidates, votes);
    cout << "The Winnder of the Election is " << candidates[findWinner(votes)] << endl;
	system("pause");
    return 0;
}

double calculatePercentage(int votes[], int vote){
    int sumOfVotes = 0;
    
    for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
        sumOfVotes += votes[i];
    }
    
    double percentage = static_cast<double>(vote) / sumOfVotes;
	return percentage*100;
}


void printResults(string candidates[], int votes[]){
    cout << "Candidate\t" << setw(15) << "Votes Received\t" << setw(8) <<"% of Total Votes" << endl;
    for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
        cout << candidates[i] << "\t" << "\t" << votes[i] << "\t" << setw(15);
        int percentage = calculatePercentage(votes, votes[i]);
        cout << percentage << "%" << endl;
        
    }
}

int findWinner(int votes[]){
    int winner = 0;
    for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
        if (votes[i] > winner)
            winner =  0 + i;
    }
    return winner;
}
The problem is in findWinner(...). Actually the variable winner is the index of the array, but on line 53 you wrongfully compare the index with the content of the field (votes).

So change line 53: if (votes[i] > votes[winner])
Topic archived. No new replies allowed.