Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
class Voter
{
public:
Voter(int id, string votes);
int get_id() const;
char get_raw_vote (int idx);
int get_vote_count() const;
string get_mayor_vote() const;
string get_prop_17_vote() const;
string get_measure_1_vote() const;
string get_measure_2_vote() const;
private:
int id;
string votes;
} ;
void write_percentage (ostream& out, int count, int total);
int main()
{
vector<Voter> voters;
ifstream list;
int id, total_voters, search_id;
bool found;
string vote_str;
int counts[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0};
list.open("List.txt");
if (list.fail())
{
cout << "Error" << endl;
exit(1);
}
while (!list.eof())
{
list >> id;
list >> vote_str;
Voter v (id, vote_str);
voters.push_back(v);
}
total_voters = voters.size();
for (int i = 0; i < total_voters; i++)
{
Voter v = voters[i];
for (int j = 0; j < 9; j++)
{
counts[v.get_raw_vote(j) - 'A']++;
}
}
cout << "1. Mayor" << endl;
cout << " A. Pincher, Penny: ";
write_percentage( cout, counts[0], total_voters);
cout << endl;
cout << " B. Dover, Skip : ";
write_percentage(cout, counts[1], total_voters);
cout << endl;
cout << " C. Perman, Sue : ";
write_percentage(cout, counts[2], total_voters);
cout << endl;
cout << "2. Proposition17" << endl;
cout << " D. Yes:";
write_percentage(cout, counts[3], total_voters);
cout << endl;
cout << " E. No:";
write_percentage(cout, counts[4], total_voters);
cout << endl;
cout << "3.Measure 1 " << endl;
cout << " F. Yes:";
write_percentage(cout, counts[5], total_voters);
cout << endl;
cout << " G. No:";
write_percentage(cout, counts[6], total_voters);
cout << endl;
cout << "4. Measure 2" << endl;
cout << " H. Yes:";
write_percentage(cout, counts[7], total_voters);
cout << endl;
cout << " I. No:";
write_percentage(cout, counts[8], total_voters);
cout << endl;
cout << "Enter the voter id";
cin >> search_id;
found = false;
for (int i = 0; (i < total_voters) && (! found); i++)
{
if (voters[i].get_id() == search_id)
{
found = true;
cout << "Voter" << search_id << " 's votes: " << endl;
cout << " 1. Mayor"
<< voters[i].get_mayor_vote() << endl;
cout << " 2. Proposition 17: "
<< voters[i].get_prop_17_vote() << endl;
cout << "3. Measure 1: "
<< voters[i].get_measure_1_vote() << endl;
cout << " 4. Measure 2: "
<< voters[i].get_measure_2_vote() << endl;
}
}
return 0;
}
void write_percentage(ostream& out, int count, int total)
{
out.setf(ios::fixed);
out.setf(ios::showpoint);
out.precision(2);
out << (static_cast<double>(count * 100) /total) << "%";
}
Voter::Voter(int voter_id, string voter_votes)
{
id = voter_id;
votes = voter_votes;
}
int Voter::get_id() const
{
return id;
}
char Voter::get_raw_vote(int idx)
{
return votes[idx];
}
int Voter::get_vote_count() const
{
return votes.length();
}
string Voter::get_mayor_vote() const
{
string mayor;
char raw;
raw = votes[0];
if (raw == 'A')
{
mayor = "Pincher, Penny";
}
else if (raw == 'B')
{
mayor = "Dover, Skip";
}
else if ( raw == 'C')
{
mayor = "Perman, Sue";
}
else
{
mayor = " (Invalid vote)";
}
return mayor;
}
string Voter::get_prop_17_vote() const
{
if (votes[1] == 'D')
{
return "Yes";
}
else {
return "No";
}
}
string Voter:: get_measure_1_vote() const
{
if (votes[2] == 'F')
{
return "Yes";
}
else {
return "No";
}
}
string Voter::get_measure_2_vote() const
{
if (votes[3] == 'H')
{
return "Yes";
}
else {
return "No";
}
}
Thank you for any help you can provide.