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;

	total= totalVote(votes);

	for (j=0; j<5; j++)

	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;


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

	return total;


int win_index(int votes[], int)

	int i, j, win;


	for (j=0; j<5; j++)
		if (votes[j]<i)
			win= i;
			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?


13 Years
Discussion Span
Last Post by Scuuba

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