Election votes Calculator in c++ using oop
Elections Votes Calculator
Pakistan is going to conduct elections this year for the seat of Member of Provisional Assembly (MPA) the candidates that are taking part in election are:
1. Imran Khan
2. Sheikh Rasheed
3. Nawaz Shareef
4. Maryam Nawaz
5. Benazir Bhutto
Each of these participants earned votes from people. On the number of votes earned it is to be decided who won the elections.
Considering this scenario draw a class diagram and then implement code in C++ having classes with data members, member functions and constructors as identified in class diagram. Sample outputs attached below.
//Output #1
Enter Candidate #1 Name: Imran Khan
Votes Earned by Imran: 8806
Enter Candidate #2 Name: Sheikh Rasheed
Votes Earned by Sheikh: 5056
Enter Candidate #3 Name: Nawaz Shareef
Votes Earned by Nawaz: 4500
Enter Candidate #4 Name: Maryam Nawaz
Votes Earned by Maryam: 5026
Enter Candidate #5 Name: Benazir Bhutto
Votes Earned by Benazir: 6602
************************ Result of Election Board ************************
Name Votes Percentage
Imran Khan 8806 29%
Sheikh Rasheed 5056 16%
Nawaz Shareef 4500 15%
Maryam Nawaz 5026 16%
Benazir Bhutto 6602 22%
Imran Khan is the Winner of the Elections
//Output #2
Enter Candidate #1 Name: Imran Khan
Votes Earned by Imran: 8806
Enter Candidate #2 Name: Sheikh Rasheed
Votes Earned by Sheikh: 5056
Enter Candidate #3 Name: Nawaz Shareef
Votes Earned by Nawaz: 4500
Enter Candidate #4 Name: Maryam Nawaz
Votes Earned by Maryam: 5026
Enter Candidate #5 Name: Benazir Bhutto
Votes Earned by Benazir: 8806
************************ Result of Election Board ************************
Name Votes Percentage 32194
Imran Khan 8806 27%
Sheikh Rasheed 5056 15%
Nawaz Shareef 4500 13%
Maryam Nawaz 5026 15%
Benazir Bhutto 8806 27%
There is a tie between Imran and Benazir
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Election{
string mem[6];
bool swap[5];
float per[6];
int vot[6];
public:
Election(){
for (int i = 0; i < 5; i++)
{
swap[i] = 0;
mem[i] = '0';
per[i] = 0;
}
}
Election(string a, int aa, string b, int aa1, string c, int aa2, string d, int aa3, string e, int aa4){
for (int i = 0; i < 5; i++)swap[i] = 0;
mem[0] = a; mem[1] = b; mem[2] = c; mem[3] = d; mem[4] = e;
vot[0] = aa; vot[1] = aa1; vot[2] = aa2; vot[3] = aa3; vot[4] = aa4;
}
void in()
{
int sum=0;
for (int i = 0; i<5; i++)
{
sum = sum + vot[i];
}
for (int i = 0; i<5; i++)
{
per[i] = (vot[i]*100 / sum);
}
show();
string max; int max1; float max2;
max1=vot[0];
for (int j =1; j < 5; j++)
{
if (max1<vot[j])
{
max = mem[j]; max1 = vot[j]; max2 = per[j];
}
}
cout << "Winner is:" << max << endl;
cout << "Votes are:" << max1 << endl;
cout << "Percentage is:" << max2 << endl;
process();
check();
}
void show()
{
cout << "*******************Result of Election Board***************************" << endl;
cout << "Name Votes percentage" << endl;
for (int i = 0; i < 5;i++)
cout << left<<setw(35) << mem[i]<<setw(35)<<vot[i] <<setw(10)<< per[i] << endl;
cout << endl;
}
void check()
{
for (int i = 0; i < 5; i++)
{
if (swap[i] == 1)
{
cout << "Votes are same for:" << mem[i] << endl;
}
}
}
void process()
{
for (int i = 0; i < 5; i++)
{
for (int j = i+1; j < 5; j++)
{
if (vot[i] == vot[j])
{
swap[i] = 1;
swap[j] = 1;
}
}
}
}
};
int main()
{
string a1, a2, a3, a4, a5; int v1, v2, v3, v4, v5;
cout << "Enter the name of 1st member:"; getline(cin, a1);
cout << "Enter the votes for 1st member:"; cin >> v1; cin.ignore();
cout << "Enter the name of 2nd member:"; getline(cin, a2);
cout << "Enter the votes for 2nd member:"; cin >> v2; cin.ignore();
cout << "Enter the name of 3rd member:"; getline(cin, a3);
cout << "Enter the votes for 3rd member:"; cin >> v3; cin.ignore();
cout << "Enter the name of 4th member:"; getline(cin, a4);
cout << "Enter the votes for 4th member:"; cin >> v4; cin.ignore();
cout << "Enter the name of 5th member:"; getline(cin, a5);
cout << "Enter the votes for 5th member:"; cin >> v5; cin.ignore();
Election e1(a1, v1, a2, v2, a3, v3, a4, v4, a5, v5);
e1.in();
getchar();
getchar();
return 0;
}
Comments
Post a Comment