ok, so I'm supposed to write a program which allows the user to input 5 candidate names and the number of votes they receive. The program should the spit out the names, number of votes, percentage of votes received, the total votes, and the winner of the election. This is the code:

#include <iostream>
#include <cstring>
#include <iomanip>

using namespace std;


int totalVote(int votes[]);
int win_index(int votes[], int);


int main()
{

    string candidate[5];
	int votes[5];


	int total, j, win;

	

	cout<<"Please enter the candidate and the number of votes they received."<<endl;

	
	for (j=0; j<5; j++)
	{
		cin>> candidate[j]>> votes[j];
	}

	
	
	cout<<setw(20)<<left<<"Candidate"<<setw(20)<<"Votes Received"<<setw(20)<<"% of Total Votes"<<endl;
	cout<<"---------------------------------------------------------"<<endl<<endl;
	

	total= totalVote(votes);

	for (j=0; j<5; j++)
	{
		cout<<setw(20)<<left<<candidate[j]<<setw(20)<<votes[j]<<setw(20)<<(static_cast<float>(votes[j])/total)*100<<endl;
	}

	cout<<"Total Votes: "<<total<<endl;

	win = win_index (votes, 5);
	cout<<"The winner is: "<<candidate[win]<<endl;

	return 0;


}


int totalVote(int votes[])
{
	int i, total;

	total=0;

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

	return total;

}



int win_index(int votes[], int)
{

	int i, j, win;

	i=0;

	for (j=0; j<5; j++)
	{
		if (votes[j]<i)
			win= i;
		else
			win= votes[j];
	}

	return win;
}

The compiler is telling me that: error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >.

Does anyody know what this means?
Also, I don't think the last function is quite right... any suggestions?

Thanks

yep, that did it, however when I run it, it says the memory can not be read. Do you know what this means?

What's the point of the int parameter in the win_index function? You should give it a name and use it, or remove it entirely..

Also, it would be far more efficient to add up the total votes as you are receiving them in then to iterate through them later...

Also, win_index function doesn't make any sense to begin with - the winner is always the last candidate unless one of your candidates has negative votes...

The int parameter has scince been removed. My professor gave me a shell to work with and it had the function with 2 parameters in it.

This article has been dead for over six months. Start a new discussion instead.