I am working on an assignment and I am suppose to use vectors to find the sum and winner but I do not understand c++ so this is confusing. The program runs but it is giving me the wrong winner, it displays the winner as who ever is entered first.

``````//Data:
/*
Johnson 5000
Miller 4000
Duffy 6000
Robinson 2500
Sam 1800
*/

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

int winnerIndex(vector<int> list, int size);

int main()
{
vector<string> candidates(5);

cout << fixed << showpoint;
cout << setprecision(2);
cout << "Enter candidate's name and the votes received by the candidate."
<< endl;

for (int i = 0; i < 5; i++)

for (int i = 0; i < 5; i++)
cout << left << setw(8) << candidates[i]
<< right << " " << setw(10) << votes[i] << "   "
<< setw(15)
<< endl;

cout << "Total          " << totalVotes << endl;

cout << "The Winner of the Election is "

system("pause");
return 0;
}

{
int sum = 0;

for (int i = 0; i < 5; i++)
sum += list[i];

return sum;
}

int winnerIndex(vector<int> list, int size)
{
int winInd = 0;

for (int i = 0; i < 5; i++)
{
if (list[i] < winInd)
{
winInd = list[i];

}
}

return winInd;
}``````
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by packluv12

Line 69-73 should be

``````if (list[i] > list[winInd])
{
winInd = i;
}``````

What you originally have is if the votes are less then zero then set the index to the number of votes. What I wrote says is if the votes at this index are more than the votes at the first index then set the index to this one.

Thank you, C++ is so confusing and I have no programming experience so its like learning a foreign language

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.