Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Project 3
- // Name: Jay Mellick
- // Netid: jtm79
- // COSC 051
- // Section: 2
- // In accordance with the class policies and Georgetown's Honor Code,
- // I certify that I have neither given nor received any assistance
- // on this project with the exceptions of the lecture notes and those
- // items noted below.
- //
- // Any help items:
- //
- // Description: This program lets the user perform different tasks with county election files.
- //
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- //First function to print the results of the county election file
- void print_results(const std::vector<string> & countyNameVector, const std::vector<int> & countyNCount, const vector<int> & countyFCount, const std::vector<int> & countyOCount, int & NTotal, int & FTotal, int & OTotal)
- {
- cout << endl;
- cout << endl;
- cout << endl;
- cout << "County" << setw(20) << "Nullifier" << setw(20) << "Fieldite" << setw(20) << "Third Party" << endl;
- cout << "----------------------------------------------------------------------" << endl;
- //CountyNameVector is a vector which holds all the county names of the files that have been loaded
- for(int i = 0; i < countyNameVector.size(); i++)
- {
- cout << countyNameVector[i] << setw(20) << countyNCount[i] << setw(20) << countyFCount[i] << setw(20) << countyOCount[i] << endl;
- //This is totalling up the tallys of votes for each party
- NTotal = NTotal + countyNCount[i];
- FTotal = FTotal + countyFCount[i];
- OTotal = OTotal + countyOCount[i];
- }
- cout << "-----------------------------------------------------------------------" << endl;
- cout << "Total" << setw(20) << NTotal << setw(20) << FTotal << setw(20) << OTotal << endl << endl << endl << endl << endl;
- }
- //Function that searches for a specific county name in the loaded files
- void search_county(const std::vector<string> & countyNameVector, const std::vector<int> & countyNCount, const vector<int> & countyFCount, const std::vector<int> & countyOCount)
- {
- string searchname;
- cout << "Enter the county file to search for: ";
- cin >> searchname;
- for(int i = 0; i < countyNameVector.size(); i++)
- {
- if(searchname == countyNameVector[i])
- {
- cout << endl;
- cout << endl;
- cout << "County" << setw(20) << "Nullifier" << setw(20) << "Fieldite" << setw(20) << "Third Party" << endl;
- cout << "-----------------------------------------------------------------------" << endl;
- cout << countyNameVector[i] << setw(20) << countyNCount[i] << setw(20) << countyFCount[i] << setw(20) << countyOCount[i] << endl;
- cout << "-----------------------------------------------------------------------" << endl;
- cout << endl;
- cout << endl;
- return;
- }
- }
- cout << endl;
- //if no county name is found, then it returns a message
- cout << "No county was found!" << endl << endl;
- }
- //Function that lets the user add a county election file to the program
- void add_county_election_file(std::vector<string> & countyNameVector, std::vector<int> & countyNCount, vector<int> & countyFCount, std::vector<int> & countyOCount, int & NCount, int & FCount, int & OCount)
- {
- char vote;
- string filename;
- string countyName;
- NCount = 0;
- FCount = 0;
- OCount = 0;
- cout << "Enter the county file to process: ";
- cin >> filename;
- std::ifstream input((filename).c_str());
- if(input.is_open())
- {
- input >> countyName;
- //adds the county name to the vector of names
- countyNameVector.push_back(countyName);
- while(input >> vote)
- {
- if(vote == 'N' || vote == 'n')
- {
- NCount = NCount + 1;
- }
- else if(vote == 'F' || vote == 'f')
- {
- FCount = FCount + 1;
- }
- else
- {
- OCount = OCount + 1;
- }
- }
- //adds the count of party votes for the current file to a vector
- countyNCount.push_back(NCount);
- countyFCount.push_back(FCount);
- countyOCount.push_back(OCount);
- }
- else
- {
- cout << endl;
- cout << "File not found!" << endl;
- cout << endl;
- }
- }
- //function to get which choice the user is selecting from the menu
- char get_menu_choice()
- {
- cout << "Add a county election file A" << endl;
- cout << "Show election totals on screen P" << endl;
- cout << "Search for county results S" << endl;
- cout << "Exit the program Q" << endl;
- cout << "Please enter your choice: ";
- char selection;
- cin >> selection;
- return selection;
- }
- int main()
- {
- vector <string> countyNameVector;
- vector <int> countyNCount;
- vector <int> countyFCount;
- vector <int> countyOCount;
- int NCount = 0;
- int FCount = 0;
- int OCount = 0;
- int NTotal = 0;
- int FTotal = 0;
- int OTotal = 0;
- char selection;
- do
- {
- //this sets the user choice to a variable, which is used for our if statement
- selection = get_menu_choice();
- if(selection == 'a' || selection == 'A')
- {
- add_county_election_file(countyNameVector, countyNCount, countyFCount, countyOCount, NCount, FCount, OCount);
- }
- if(selection == 'p' || selection == 'P')
- {
- print_results(countyNameVector, countyNCount, countyFCount, countyOCount, NTotal, FTotal, OTotal);
- }
- if(selection == 'S' || selection == 's')
- {
- search_county(countyNameVector, countyNCount, countyFCount, countyOCount);
- }
- }while((selection != 'Q' && selection != 'q'));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement