1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
# include <iostream>
# include <fstream>
# include <iomanip>
# include <cstring>
# include <cstdlib>// for system("pause");
using namespace std;
const int NoCand = 5;//Global variable
const int StrSize = 20;//Global variable
void Input(char LastName[NoCand][StrSize], float NumVotes[NoCand],float &TotalVotes);
void Output(char LastName[NoCand][StrSize],float NumVotes[NoCand]);
void PercVotes(char LastName[NoCand][StrSize],float NumVotes[NoCand],
float PercentVotes[NoCand],float &TotalVotes);
int main()
{
char LastName[NoCand][StrSize];
float NumVotes[NoCand],PercentVotes[NoCand],TotalVotes=0;
Input(LastName,NumVotes,TotalVotes);
Output(LastName,NumVotes);
PercVotes(LastName,NumVotes,PercentVotes,TotalVotes);
system("pause");// Dont use it
return 0;
}
void Input(char LastName[NoCand][StrSize],float NumVotes[NoCand],float &TotalVotes)
{
ofstream Election("Election.dat",ios::app);
for(int LCV=0; LCV<NoCand; LCV++)
{
cout << "Enter last name of candidate[" << LCV << "] : ";
cin.getline(LastName[LCV],StrSize);
cout << "Enter votes received by candidate[" << LCV << "]: ";
cin >> NumVotes[LCV];cin.get();
cout << endl;//Just for a bit of organization
TotalVotes = TotalVotes + NumVotes[LCV];
Election << LastName[LCV] << '~' << NumVotes[LCV] << endl;
}Election.close();
}
void Output(char LastName[NoCand][StrSize], float NumVotes[NoCand])
{
cout << "Candidate's Last Name" << "\t\t" << "Votes Received" << endl;
ifstream Election("Election.dat");
for(int LCV=0; LCV<NoCand&&Election.good(); LCV++)
{
Election.getline(LastName[LCV],StrSize,'~');
Election >> NumVotes[LCV];
cout << left << setw(10) << LastName[LCV] << "\t\t\t" << setw(8) << NumVotes[LCV] << endl;
}Election.close();
}
void PercVotes(char LastName[NoCand][StrSize], float NumVotes[NoCand],
float PercentVotes[NoCand],float &TotalVotes)
{
ofstream ElectionRep("ElectionRep.dat",ios::app);
cout << "\nCandidate's Last Name" << "\t\t" << "Votes Received"
<< "\t\t% of total votes" << endl;
ElectionRep << "Candidate's Last Name" << "\t\t" << "Votes Received"
<< "\t\t% of total votes" << endl;
ifstream Election("Election.dat");
Election.getline(LastName[0],StrSize,'~');
Election >> NumVotes[0];
for(int LCV=0; LCV<NoCand&&Election.good(); LCV++)
{
PercentVotes[LCV] = (NumVotes[LCV]/TotalVotes)*100;
cout << left << setw(10) << LastName[LCV] << "\t\t\t" << setw(8)
<< NumVotes[LCV] << "\t\t" << PercentVotes[LCV] << endl;
ElectionRep << left << setw(10) << LastName[LCV] << "\t\t\t" << setw(8)
<< NumVotes[LCV] << "\t\t" << PercentVotes[LCV] << endl;
Election.getline(LastName[LCV],StrSize,'~');
Election >> NumVotes[LCV];
}Election.close();
// float temp_NumVotes;Unused variable
char temp_LastName[25];
for(int i=0;i<NoCand;i++)
{
for(int j=i+1;j<NoCand;j++)
if(NumVotes[j]>NumVotes[i])
{
strcpy( temp_LastName, LastName[i]);
float temp_NumVotes = NumVotes[i];
strcpy( LastName[i], LastName[j]);
NumVotes[i] = NumVotes[j];
strcpy( LastName[j], temp_LastName);
NumVotes[j] = temp_NumVotes;
}
}
int k = 0;
cout << "Total Votes \t\t" << TotalVotes << endl;
ElectionRep << "Total Votes \t\t" << TotalVotes << endl;
cout << "\nThe Winner of the election is " << LastName[k] << endl;
ElectionRep << "\nThe Winner of the election is " << LastName[k]<< endl;
ElectionRep.close();
}
|