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 sumVotes(vector<int> list, int size);
int winnerIndex(vector<int> list, int size);

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

	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++)
		cin>>candidates[i]>>votes[i];

	totalVotes = sumVotes(votes,5);

	cout << "Candidate    Votes Received   % of Total Votes" << endl;
	for (int i = 0; i < 5; i++)
		cout << left << setw(8) << candidates[i]
			 << right << " " << setw(10) << votes[i] << "   "
             << setw(15)
			 << (static_cast<double>(votes[i]) / static_cast<double>(totalVotes)) * 100
             << endl;

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

	cout << "The Winner of the Election is "
         << candidates[winnerIndex(votes, 5)] << endl;

	system("pause");
	return 0;
}

int sumVotes(vector<int> list, int size)
{
	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;
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.