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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int input_data(string*& candidates, int*& vote_count, string filename);
int lookupCandidateCount(string search_name, string names[], int votes[],
int num_candidates);
int GetElectionWinner (int votes[], int num_candidates);
void DisplayElectionStatistics (string candidates[], int votes[], int num_candidates);
int main()
{
string* candidates = nullptr;
int* votes = nullptr;
int num_candidates;
num_candidates = input_data(candidates, votes, "canidate_votes.txt");
if (num_candidates == -1)
{
cout << "Error reading data. Check the file name and try again." << endl;
}
else{
int choice = -1;
string name;
int vote_count;
int index_winner;
char c;
do
{
cout << "Main Menu" << endl;
cout << "1. Candidate Count Lookup" << endl;
cout << "2. Get Election Winner" << endl;
cout << "3. Display Election Statistics" << endl;
cout << "4. Exit" << endl;
do
{
cout << "--: " << endl;
cin >> choice;
if (choice < 1 && choice > 4)
cout << "Invalid selection. Choose again." << endl;
} while (choice < 1 && choice > 4);
switch (choice)
{
case 1:
cout << endl << endl;
cout << "Enter your candidate's last name: ";
cin.ignore();
getline(cin, name);
vote_count = lookupCandidateCount(name, candidates, votes, num_candidates);
if (vote_count == -1)
cout << "Error: could not find this candidate in the data." << endl;
else
cout << name << " drew " << vote_count << " votes in this election." << endl;
cout << endl << endl;
break;
case 2:
index_winner = GetElectionWinner (votes, num_candidates);
if (index_winner != -1)
{
cout << endl << endl;
cout << "The winner of this election was " << candidates[index_winner];
cout << " with " << votes[index_winner] << " votes." << endl;
}
else{
cout << endl << endl;
cout << "Error: Could not retrieve election winner from data... " << endl;
cout << endl << endl;
}
break;
case 3:
cout << endl << endl;
DisplayElectionStatistics(candidates, votes, num_candidates);
cout << endl << endl;
break;
case 4:
return 0;
break;
default:
cout << "\n\n Error: invalid menu choice. Choose again. \n\n";
}
} while (choice != 4);
}
}
int lookUpCandidateCount (string search_name, string names[], int votes[], int num_candidates)
{
return -1;
}
int GetElectionWinner(int votes[], int num_candidates)
{
return -1;
}
int getTotalVotes (int votes[], int num_candidates)
{
return -1;
}
void DisplayElectionStatistics(string candidates[], int votes[], int num_candidates)
{
cout << "Error: This function is not yet complete." << endl;
}
int get_fsize(string filename)
{
ifstream file;
int size = 0;
file.open(filename);
if (file.is_open())
{
string line;
while (getline(file, line))
size++;
}
else
size = -1;
file.close();
return size;
}
int input_data(string*& candidates, int*& vote_count, string filename)
{
ifstream file;
int size = get_fsize(filename);
if (size == -1)
return size;
else
{
file.open(filename);
if (file.is_open())
{
string line;
candidates = new string[size];
vote_count = new int[size];
int i = 0;
while (getline(file, line))
{
int seperator = 0;
while (line[seperator] != ',')
{
candidates [i] += line[seperator];
seperator++;
}
seperator++;
string vote_count_str = "";
while (seperator < line.size())
{
vote_count_str += line[seperator];
seperator++;
}
vote_count[i] + stoi(vote_count_str);
i++;
}
file.close();
}
else
size + -1;
return size;
}
}
|