C++ ONLY PLEASE. I have submitted this question before but the answer code did n
ID: 3675532 • Letter: C
Question
C++ ONLY PLEASE. I have submitted this question before but the answer code did not work. Below is the instruction, code, the program input and the expected output. If you can check your answer to make sure it is correct I would really appreciate it. Thanks.
22.4 C++: Elections results
Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is:
Candidate Votes Received % of Total Votes
Johnson 5000 25.91
Miller 4000 20.73
Duffy 6000 31.09
Robinson 2500 12.95
Ashtony 1800 9.33
Total 19300
The Winner of the Election is Duffy.
#include <iostream>
using namespace std;
int sumVotes(int list[], int size);
int winnerIndex(int list[], int size);
int main() {
/* Type your code here. */
return 0;
}
Input
Johnson 5000 Miller 4000 Duffy 6000 Robinson 2500 Ashtony 1800
Expected output
Enter candidate's name and the votes received by the candidate. Candidate Votes Received % of Total Votes Johnson 5000 25.91 Miller 4000 20.73 Duffy 6000 31.09 Robinson 2500 12.95 Ashtony 1800 9.33 Total 19300 The Winner of the Election is Duffy.
Input
Johnson 5000 Miller 4000 Duffy 6000 Robinson 2500 Ashtony 1800
Explanation / Answer
electionTester.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include "election.h"
using namespace std;
int main()
{
cout<<"HERE";
int age;
cin>>age;
cout<<"HERE2";
int numOfCandidates = 3;
int voteTotal =0;
int winner =0;
election elect;
ifstream inf;
inf.open("Election.txt");
ofstream outf;
outf.open("ElectionOut.txt");
if(!inf)
{
cout << " *** Input file does not exist! ***" << endl;
return 1;
}
outf << "Vincent Fiore CUS 1151 Project #3 " << endl
<< endl <<endl << endl;
outf << "Candidate" << setw(10) << "Votes" << setw(10) << "% of Votes" << endl;
outf<< "The total vote count is " << endl;
outf<< "The winner is " << endl;
while (!inf.eof() )
{
elect.readData(numOfCandidates, inf);
elect.print(outf, voteTotal, winner, numOfCandidates);
}
inf.close();
outf.close();
return 0;
}
electionImp.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include "election.h"
using namespace std;
election::election()
{
*candidatefName="";
*candidatelName="";
*votes=0;
candidatefName = new string[3];
candidatelName = new string[3];
votes = new int[3];
}
void election::readData(int numOfCandidates, ifstream &inf)
{
cout<<"READDATAHERERERER";
string canfName;
string canlName;
int vtes;
for (int i=0; i<numOfCandidates; i++)
{
inf >> canfName >> canlName >> vtes;
canfName =candidatefName[i];
canlName =candidatelName[i];
vtes = votes[i];
}
}
void election::sumVotes(int &voteTotal, int numOfCandidates)
{
}
void election::winnerIndex(int &winner, int numOfCandidates)
{
}
void election::print(ofstream& outf, int voteTotal, int winner, int numOfCandidates) const
{
cout<<"HERERERER";
outf<< "HERE";
for (int i=0; i<numOfCandidates; i++)
{
outf << candidatefName[i] << candidatelName[i] << votes[i] <<endl;
}
}
election.h
#ifndef H_election
#define H_election
#include <fstream>
#include <string>
using namespace std;
class election
{
public:
void readData(int numOfCandidates, ifstream& inf);
void sumVotes(int& voteTotal, int numOfCandidates);
void winnerIndex(int& winner, int numOfCandidates);
void print(ofstream& outf, int voteTotal, int winner, int numOfCandidates) const;
election();
private:
string *candidatefName;
string *candidatelName;
int *votes;
};
#endif
Election.txt
Bob Smith 700
Gail Jones 640
Bob Brown 810
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.