Help With a Program

closed account (SNvqM4Gy)
I need help on the functions that are obviously not finished.(Ostream,search, both of the display_votes) Any help would be greatly appreciated.

#include <iostream>
#include <fstream> // for open a vote file
#include <string> // for using string class. Refer page 471 to 475 for string class
#include <vector> // for using vector class.


using namespace std;

class Voter
{
public:
// friend functions
friend istream& operator >> (istream& ins, Voter& v);
// input a Voter from istream ins

friend ostream& operator << (ostream& outs, const Voter& v);
// output the Voter v to ostream outs. The value of v will not be changed

friend int search(vector<Voter>& v, int id);
// return the index of the voter with given id; if no voter found, return -1;

friend void sort(vector<Voter>& v);

friend void display_stats(vector<Voter>& v, ostream& outs);
// display the statistics data for vote of type.

// constructor
Voter();
// create a Voter with id 0000, and vote "XXXX"

Voter(string vote, int id);
// create a Voter with given id and vote

// member functions
int get_id();
// return the Voter's id

string get_vote(int i);
// get vote for ith question

string get_vote();
// get the whole vote as a string

void set_id(int id);
// set the Voter's id to given id

void set_vote(string vote);
// set the Voter's vote to given vote

void set_vote(int i, string vote);
// set the ith vote to be given vote

private:
int _id;
string _vote;
};

void display_votes(vector<Voter>& v, ostream& outs);
// display all Voters' voting information

void display_votes(vector<Voter>& v, int id, ostream& outs);
// display the voting information of a Voter with given id. If the Voter is not found, display not found message

void menu();

int main()
{
// declare variables
ifstream in;
vector<Voter> v;
int choice;

// open file to read
in.open("vote.txt");
if(in.fail())
{
cout << "Cannot open file to read" << endl;
exit(1);
}

// read data from file and save to vector
while(!in.eof())
{
Voter voter;
in >> voter;
v.push_back(voter);
}

// use menu to handle the data
do{
menu();
cout << "Enter your choice: ";

cin >> choice;
switch(choice)
{
case 1:
display_votes(v, cout);
break;
case 2:
display_stats(v, cout);
break;
case 3:
cout << "Enter the voter's id: ";
int id;
cin >> id;
display_votes(v, id, cout);
break;
case 4:
cout << "Thank you for using FHSU voting program! Good-bye!";
break;
default:
cout << "Wrong option. Try again";
break;
}
cout << endl;
system("pause");
}while(choice != 4);

// close file and exit
in.close();
return 0;
}

// friend function
istream& operator >> (istream& ins, Voter& v)
{
// you finish this
char op;
ins >> v._id;
ins >> op;
ins >> v._vote;
return ins;
}

ostream& operator << (ostream& outs, const Voter& v)
{
// you finish this
}

int search(vector<Voter>& v, int id)
{
// you finish this using linear search algorithm
for(int i =0; i < v.size(); i++)
{
if(v[i] == id) return i;
}
return -1;
}

void display_stats(vector<Voter>& v, ostream& outs)
{
int vote_count[] ={0, 0, 0, 0, 0, 0, 0, 0, 0};
// vote_count[0] holds the count of vote of As, vote_count[1] holds the count of vote of Bs, ..., etc

for(size_t i = 0; i < v.size(); i++) // step through the vector v
{
// for each vote, update the count of As, Bs, ..., Is. You only need one for loop total two lines of code to do so
for(int j =0; j <4; j++)
vote_count[int(v[i]._vote[j])-int('A')]++;
}

outs.setf(ios::fixed);
outs.setf(ios::showpoint);
outs.precision(2);

// display all stats
outs << "Vote for Mayor result: " << endl;
outs << "Pincher, Penny: " << 100.0*vote_count[0]/v.size() << "%\n";
// you finish the rest two candidates for Mayor
outs << "Dover, Skip: " << 100.0*vote_count[1]/v.size() << "%\n";
outs << "Perman, Sue: " << 100.0*vote_count[2]/v.size() << "%\n";

outs << "\nVote for Proposition 17: " << endl;
outs << " Yes: " << 100.0*vote_count[3]/v.size() << "%\n";
outs << " No : " << 100.0*vote_count[4]/v.size() << "%\n";

// you finish the rest stats for Measure 1 and Measure 2
outs << "\nVote for Measure 1: " << endl;
outs << " Yes: " << 100.0*vote_count[5]/v.size() << "%\n";
outs << " No : " << 100.0*vote_count[6]/v.size() << "%\n";

outs << "\nVote for Measure 2:: " << endl;
outs << " Yes: " << 100.0*vote_count[7]/v.size() << "%\n";
outs << " No : " << 100.0*vote_count[8]/v.size() << "%\n";

return;

}

void display_votes(vector<Voter>& v, ostream& outs)
{
// sort v first
sort(v);
// then step through v and display the votes
for(int i = 0; i < v.size(); i++)
{
outs << //*************
}
}

void sort(vector<Voter>& v)
{
// using select sort algorithm to sort v
int min, step;
for(int i=0; i< v.size(); i++)
{
min = i;
for(step = i+1; step < v.size(); step++)
if(step < min)
min=step;

Voter temp; // swap part
temp = v[min];
v[min] = v[step];
v[step] = temp;
}
}

void display_votes(vector<Voter>& v, int id, ostream& outs)
{
// search for id
for(int i = 0; i <4; i++){
if(v[i] == id)
return; //**************
else
cout << "";
}

// if id not found, display error message
// else display the vote by the voter whose id is the given id
}

void menu()
{
system("CLS");
cout << "********************************\n";
cout << "* Menu *\n";
cout << "* 1. Display Voting File *\n";
cout << "* 2. Display Voting Stats *\n";
cout << "* 3. Display Particular Vote *\n";
cout << "* 4. Exit *\n";
cout << "********************************\n";
}


// constructor
Voter::Voter(): _id(0), _vote("XXXX")
{
// initialize the id to be 0 and vote to be "XXXX"
}

Voter::Voter(string vote, int id):_id(id), _vote(vote)
{
// initialize the id and votes as the given values
}

// member functions
int Voter::get_id()
{
// return the id
return _id;

}

string Voter::get_vote(int i) // get vote for ith question
{
// return the vote for ith item as a string
return _vote.substr(i-1, 1);

}


string Voter::get_vote() // get the whole vote as a string
{
// return whole vote as a string
return _vote.substr();

}

void Voter::set_id(int id)
{
// set the id as given id
_id=id;

}

void Voter::set_vote(string vote)
{
// set the vote as given vote
_vote = vote;

}

void Voter::set_vote(int i, string vote)
{
// set the vote for ith item as the given vote.
//erase ith vote
_vote.erase(i-1, 1);
_vote.insert(i-1, vote);
//insert new vote

}
Topic archived. No new replies allowed.