You are on page 1of 4

PROJECT OBJECT ORIENTED PROGRAMMING 2021

NAME: MARYAM KHALIL


CLASS: BSIT
SECTION: 2A
ENROLLNMENT :01-135202-037

PROGRAM SOUIRCE CODE


#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<sstream>
using namespace std;
class Voter
{
public:

Voter(string name = "", int votes = 0)


{
this->name = name;
this->numVotes = votes;
}
void setName(string name)
{
this->name = name;
}
string getName()
{
return name;
}
int getVote()
{
return numVotes;
}
void addVote()
{
numVotes++;
}

private:
string name;
int numVotes;
};
void readVoters(vector<Voter>&);
int findVoter(vector<Voter>, string);
void printTopp5Voters(vector<Voter>v);
int getTotalVotes(vector<Voter>v);
int main()
{
vector<Voter>voters;
readVoters(voters);
string choice;
do
{
cout << " name of the candidate you like : ";
string name;
getline(cin, name);
int pos = findVoter(voters, name);
if (pos >= 0)
{
cout << " Candidate is already is the list " << voters.at(pos).getVote() <<
" people has voted him before . Do you want to vote him. (y/n)? ";
cin >> choice;
if (choice == "y")
{
voters.at(pos).addVote();
cout << " Recent votes : " << voters.at(pos).getVote() << endl;
cout << "Thanks for voting. ";
break;
}
}
else
{
cout << " Candidate you voted is not in the list . AS per your desire do
you want to include him in the list of candidates (y/n)?: ";
cin >> choice;
if (choice == "y")
{
Voter temp(name, 1);
voters.push_back(temp);
cout << "The candidate Wasim Akram is included in the list. His recent
votes : " << temp.getVote() << endl;
cout << "Thanks for the vote. ";
break;
}
}
cout << "will you like to vote another candidate (y/n)? ";
cin >> choice;
cin.ignore(2, '\n');
} while (choice == "y");
cout << " Results:" << endl;
printTopp5Voters(voters);
cout << " Total NO : OF persons already voted are: " << getTotalVotes(voters) <<
endl;
system("pause");
return 1;
}
void readVoters(vector<Voter>& v)
{
ifstream inFile("candidates.txt");
string line, value, name, delimiter = ", ";
int vote;
getline(inFile, line);
while (!inFile.eof())
{
getline(inFile, line);
size_t pos = 0;
pos = line.find(delimiter);
value = line.substr(0, pos);
line.erase(0, pos + delimiter.length());
pos = line.find(delimiter);
value = line.substr(0, pos);
name = value;
line.erase(0, pos + delimiter.length());
vote = stoi(line);
v.push_back(Voter(name, vote));
}
}
int findVoter(vector<Voter> v, string name)
{
for (unsigned i = 0; i < v.size(); i++)
if (v[i].getName() == name)
return i;
return -1;
}
void printTopp5Voters(vector<Voter>v)
{
for (unsigned i = 0; i < v.size() - 1; i++)
{
for (unsigned j = i + 1; j < v.size(); j++)
{
if (v.at(i).getVote() < v.at(j).getVote())
{
Voter temp = v.at(i);
v.at(i) = v.at(j);
v.at(j) = temp;
}
}
}

cout << "Five candidantes on top:" << endl;


for (unsigned i = 0; i < 5; i++)
{
cout << (i + 1) << ". " << v.at(i).getName() << ", " << v.at(i).getVote() <<
endl;
}

}
int getTotalVotes(vector<Voter>v)
{
int sum = 0;
for (unsigned i = 0; i < v.size(); i++)
sum += v[i].getVote();
return sum;
}

OUTPUT

You might also like