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
|
#include "iostream"
#include "iomanip"
#include "fstream"
#include "cstdlib"
#include "string"
using namespace std;
const int NUM_OF_VOTES = 5;
void openAndStore(ifstream& in1, string names[],int votes[]);
void calculateLargestVote(const string names[], const int votes[], int length);
void calculatePercentages(const int votes[], double percent[], int length);
void printResults(const string names[], const int votes[], const double percent[], int length);
int main(){
string names[NUM_OF_VOTES];
int votes[NUM_OF_VOTES];
double percents[NUM_OF_VOTES];
ifstream input;
openAndStore(input,names,votes);
calculatePercentages(votes,percents,NUM_OF_VOTES);
printResults(names,votes,percents,NUM_OF_VOTES);
calculateLargestVote(names,votes,NUM_OF_VOTES);
}
void openAndStore(ifstream& in1, string names[], int votes[]){
in1.open("F:\\school\\assign5input.txt");
int length = 0;
while(in1 && length < 5){
length++;
in1 >> names[length] >> votes[length];
}
}
void calculateLargestVote(const string names[], const int votes[], int length){
int largest;
string temp;
for(largest = 0; largest < length; largest++){
if(votes[largest] > votes[length]){
largest = length;
temp = names[largest];
cout << "The winner of the election is " << temp << "with" << largest << "votes";
}
length++;
}
}
void calculatePercentages(const int votes[], double percent[], int length){
int per, temp;
for(int i = 0; i < length; i++){
temp =+ votes[i];
}
for(int i = 0; i < length; i++){
percent[i] = (votes[i]/temp) * 100;
}
}
void printResults(const string names[], const int votes[], const double percent[], int length){
cout << "Candidate" << setw(5) << "Votes Recieved" << setw(5) << "% of Total Votes" << endl;
cout << endl;
for(int i = 0; i < length; i++){
cout << names[i] << setw(5) << votes[i] << setw(5) << percent[i] << endl;
}
}
|