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();
}