Hi, i have to write a program to allow the user to input 5 candidate names and the number of votes they received, then it calculated what percent of the vote each candidate received and then ten tells the user who won. I think I've got a code that will work but i have one small problem. On lines 19 and 37 of my test file...

candidate.insert(i, name);

and

votes.insert(i, vote);

both candidate and votes say the are undeclared so for some reason they aren't being accessed in the header file and i don't know why, am I using .insert wrong, please help I don't have much time.

Here's the rest of the code...

election List Header

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

using namespace std;

class electionList
{
public:
	void print() const;
	void voteTotal();
	void calcPercentage();
	void calcWinner();
	vector<int>::iterator votesIter;
	vector<double>::iterator percentageIter;
	vector<string>::iterator candidateIter;
protected:
	vector<int> votes;
	vector<double> percentage;
	vector<string> candidate;
	int total;
};

election List Imp Source File

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

using namespace std;

#include "election.h"

//Function to print the final table showing all the stats.
void electionList::print() const
{
	cout<<setw(30)<<"Candidate"<<setw(30)<<"Votes Recived"<<setw(30)<<"% of Votes"<<endl;

	for (int i=0; i<5; i++)
	{
		cout<<setw(30)<<candidate[i]<<setw(30)<<votes[i]<<setw(30)<<percentage[i]<<endl;
	}

	cout<<setw(30)<<"Total"<<setw(30)<<total<<endl;
}

//Calculate the total amount of votes recived by the candidates.
void electionList::voteTotal()
{
	total = (votes[0]+votes[1]+votes[2]+votes[3]+votes[4]);
}

//Calculates the percentage of votes each candidate got.
void electionList::calcPercentage()
{
	for (int i=0; i<5; i++)
	{
		percentage[i] = ((votes[i]/total)*(100));
	}
}

//Compares the number of votes to see who won the election.
void electionList::calcWinner()
{
	string winner = candidate[0];
	int max = votes[0];

	for (int i=0; i<5; i++)
	{
		if(votes[i]>max)
		{
			max = votes[i];
			winner = candidate[i];
		}
	}
}

election List Test Source File

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

using namespace std;

#include "election.h"

int main()
{
	electionList election;

	string name;
	cout<<"Enter the last names of the five candidates: ";
	for (int i=0; i<5; i++)
	{
		cin>>name;
		candidate.insert(i, name);
	}

	int vote;
	cout<<"Now enter the votes that each candidate recieved in the same order you entered the candidate: ";
	for (int i=0; i<5; i++)
	{
		cin>>vote;
		votes.insert(i, vote);
	}

	election.voteTotal();
	election.calcPercentage();
	election.calcWinner();
	election.print();
}

Hi,

candidate and votes are both protected members of your class so you cannot access them in main. you should write access and set functions to your class.

so say you add a function to your class

AddCandidate(std::string name)
{
   candidates.push_back(name); //adds candiate to the list
}

this way in main you can add a candidate, the function works because candidate is protected/private so the class can add elements in a way as above but main can only use public methods/variables of your class.

The same will have to be done for votes and you will also need functions to read back the candidates and votes from main, again with public access functions.

Cool thanks, I'll give it a try.

cool that fixed the error and works great one other question though if anyone can help, the function to find the total is working but not storing the value, if i print from within the function it calculates it right, but if i try to call the value it returns in the print function all i get is -858993460

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.