Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Problem Write a program that reads from a text file the last names of five c

ID: 3582374 • Letter: C

Question

C++ Problem

Write a program that reads from a text file the last names of five candidates in
a local election and the votes received by each candidate. The program should
then output each candidate’s name, votes received by that candidate, and the
percentage of the total votes received by the candidate. Your program should
also output the winner of the election.

1. read the candidate names and the candidate votes received from a file

2. the file contains at least two entries for each candidate

3. the entries are in no particular order

4. use a binary search tree to store the candidates' names and vote totals

5. A candidate node of the tree is a string and an integer

e.g.

struct Candidate

{

string name;

int votes;

}

An input file may look like

Johnson 2000

Miller 1000

Duffy 1000

Robinson 1500

Sam 900

Sam 500

Miller 2000

Duffy 4000

Johnson 2000

Robinson 1000

Sam 400

Johnson 1000

Miller 1000

Duffy 1000

A sample output is as follows:
Candidate Votes Received % of Total Votes
Johnson 5000 25.91
Miller 4000 20.72
Duffy 6000 31.09
Robinson 2500 12.95
Sam 1800 9.33
Total 19300
The Winner of the Election is Duffy

Explanation / Answer

Solution:

/***
Write a program that reads from a text file the last names of five candidates in
a local election and the votes received by each candidate. The program should
then output each candidate’s name, votes received by that candidate, and the
percentage of the total votes received by the candidate. Your program should
also output the winner of the election.
1. read the candidate names and the candidate votes received from a file
2. the file contains at least two entries for each candidate
3. the entries are in no particular order
4. use a binary search tree to store the candidates' names and vote totals
5. A candidate node of the tree is a string and an integer
***/
#include <fstream>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
int AddFirst(int sum, const pair<int, string>& rhs)
{
return sum + rhs.first;
}
struct Candidate
{
const int total;
Candidate(int t) : total(t) {}
void operator()(const pair<int, string>& p) const {
cout << setw(9) << p.second
<< ' ' << setw(14) << p.first
<< ' ' << setw(16) << fixed << setprecision(2) << 100.*p.first/total << ' ';
}
};
multimap<int, string> read_votes_from_file(const std::string& name)
{
multimap<int, string> votes;
ifstream f(name.c_str());
string s;
while(getline(f, s)) {
istringstream is(s);
int n;
if(is >> s >> n) votes.insert(make_pair(n, s));
}
return votes;
}
int main()
{
multimap<int, string> votes = read_votes_from_file("input.txt");
int total = accumulate(votes.begin(), votes.end(), 0, AddFirst);
cout << "Candidate Votes Received % of Total Votes "
<< "--------- -------------- ---------------- ";
for_each(votes.begin(), votes.end(), Candidate(total));
cout << "--------- -------------- ---------------- "
<< "Total " << total << " ";
if( votes.count(votes.rbegin()->first) == 1 )
cout << "The Winner of the Election is " << votes.rbegin()->second << endl;
else
cout << "The voting tied!" << endl;
}

Sample Output:

Candidate Votes Received % of Total Votes
--------- -------------- ----------------
Ashtony 1800 9.33
Robinson 2500 12.95
Miller 4000 20.73
Johnson 5000 25.91
Duffy 6000 31.09
--------- -------------- ----------------
Total 19300
The Winner of the Election is Duffy

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote