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')]++;
}
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;
// 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