#include #include #include #include #include using namespace std; // First, just do the loop that displays choices and prints the menu. Get the choice and print it out. // Second, write the function that loads the election file. To do this, first make sure that the // counts that were read in are correct. Once you are sure, then stick the information calculated // into the appropriate vectors // Third, print out the results neatly // Fourth, now add doing the vote summation and print those as well. // Fifth, now add searching. One function can search for and print all at once // Function prototypes char get_menu_choice(); void add_county_election_file(const string, vector &, vector &, vector &, vector &); void search_county(const string, const vector &, const vector &, const vector &, const vector &); void print_results(const vector, const vector, const vector, const vector); int sum_vector(const vector); void print_line(); void vertical_space(int); // Start the program, loop while allowing choices from the menu int main(){ bool finished = false; char choice; vector county_names; vector nullifier_votes; vector fieldite_votes; vector third_party_votes; string filename, county; do { choice = get_menu_choice(); switch (choice){ case 'Q': case 'q': finished = true; break; case 'A': case 'a': cout << "Enter the county file to process: "; cin >> filename; add_county_election_file(filename, county_names, nullifier_votes, fieldite_votes, third_party_votes); break; case 'P': case 'p': print_results(county_names, nullifier_votes, fieldite_votes, third_party_votes); break; case 's': case 'S': cout << "Enter the county file to search for: "; cin >> county; search_county(county, county_names, nullifier_votes, fieldite_votes, third_party_votes); break; default: cout << "Invalid option: " << choice < & counties, vector & nullifiers, vector & fieldites, vector & other){ ifstream countyFile; string countyName; char vote; int nullifierVotes = 0, fielditeVotes = 0, otherVotes = 0; countyFile.open(filename.c_str()); if (!countyFile){ cout << "File not found: " << filename << endl; return; } // Read in the first line with the county name getline(countyFile, countyName); while (countyFile >> vote){ switch (vote){ case 'N': case 'n': nullifierVotes ++; break; case 'F': case 'f': fielditeVotes ++; break; default: otherVotes++; } } counties.push_back(countyName); nullifiers.push_back(nullifierVotes); fieldites.push_back(fielditeVotes); other.push_back(otherVotes); return; } //////////////// // Returns the choice the user selected after printing // the menu char get_menu_choice(){ char choice; cout << setw(45) << "Add a county election file" << setw (10) << "A" << endl; cout << setw(45) << "Show election totals on screen" << setw(10) << "P" << endl; cout << setw(45) << "Search for county results" << setw(10) << "S" << endl; cout << setw(45) << "Exit the program" << setw (10) << "Q" << endl; cout << endl; cout << "Please enter your choice: "; cin >> choice; return choice; } /////// // Prints out the election results neatly, including the sums void print_results (const vector counties, const vector nullifier, const vector fieldite, const vector other){ vertical_space(5); cout << setw(20) << " " << setw(15) << "County" << setw(15) << "Nullifier" << setw(15) << "Fieldite" << setw(15) << "Third Party" << endl; print_line(); for (int i = 0; i < counties.size(); i++){ cout << setw(35) << counties[i] << setw(15) << nullifier[i] << setw(15) << fieldite[i] << setw(15) << other[i] << endl; } print_line(); cout << setw(20) << " " << setw(15) << "Total" << setw(15) << sum_vector(nullifier) << setw(15) << sum_vector(fieldite) << setw(15) << sum_vector(other) << endl; vertical_space(5); } //////// // a quick little function to add up a vector's contents int sum_vector(const vector toSum){ int sum = 0; for (int i = 0; i < toSum.size(); i ++){ sum += toSum[i]; } return sum; } ///////// // Searches for the results for a particular county // Use const with reference parameters for speed! void search_county(const string county, const vector & counties, const vector & nullifiers, const vector & fieldites, const vector & other){ for (int i = 0; i< counties.size(); i++){ if (counties[i] == county){ vertical_space(5); cout << setw(20) << " " << setw(15) << "County" << setw(15) << "Nullifier" << setw(15) << "Fieldite" << setw(15) << "Third Party" << endl; print_line(); cout << setw(35) << counties[i] << setw(15) << nullifiers[i] << setw(15) << fieldites[i] << setw(15) << other[i] << endl; print_line(); vertical_space(5); // Return once we find it and print it return; } } // if we reach here, we never found what we were looking for. cout << setw(35) << "County: " << county << " not found" << endl; } /////////// //Print a line for formatting void print_line(){ cout << setw(20) << " "; for (int i = 0; i < 60; i++){ cout << "-"; } cout << endl; } /////////// // put some space in the output void vertical_space(int size){ for (int i = 0; i < size; i++){ cout << endl; } }