Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- class countyElectionResults{
- friend std::ostream &operator<<(ostream &, countyElectionResults);
- public:
- countyElectionResults();
- countyElectionResults(string filename);
- bool loadFile(string filename);
- string getCountyName();
- vector<char> getVotes();
- void showVotes();
- int getNullifierVotes();
- int getFielditeVotes();
- int getOtherVotes();
- int getTotalVotes();
- private:
- string countyName;
- vector<char> votes;
- int nullifierVotes;
- int fielditeVotes;
- int otherVotes;
- int totalVotes;
- // A private function that counts all the votes stored in the
- // votes vector
- void countVotes();
- };
- class stateElectionResults {
- public:
- // Asks the user for a file name, then loads that file into
- // a countyElectionObject from that file and places it into a vector
- void addCountyFile();
- // Prints the results from all countyElection results
- void showAllCountyResults();
- // Prints the results from one county whose name is passed as a string
- void showCountyResults(string);
- // Prints votes from one county whose name is passed as a string
- void showCountyVotes(string);
- // Prints the names of the counties that are stored in the vector.
- void listCounties();
- private:
- vector<countyElectionResults> stateResults;;
- };
- //overloading the operator
- ostream &operator<<(ostream &output, countyElectionResults county_results)
- {
- output << county_results.countyName;
- output << county_results.nullifierVotes;
- output << county_results.fielditeVotes;
- output << county_results.otherVotes;
- output << county_results.totalVotes;
- }
- // Constructor - just initializes internal variables
- countyElectionResults::countyElectionResults()
- {
- nullifierVotes = 0;
- fielditeVotes = 0;
- otherVotes = 0;
- totalVotes = 0;
- }
- // Constructor - initializes internal variables and loads data from file
- // string parameter is filename
- countyElectionResults::countyElectionResults(string filename)
- {
- filename = " ";
- }
- // Loads data from a file, string parameter is the filename
- // returns true if it worked, and false if not
- bool countyElectionResults::loadFile(string filename)
- {
- char vote;
- std::ifstream input((filename).c_str());
- if(!input.good())
- {
- return false;
- }
- getline(input, countyName);
- //counts each vote
- while(input >> vote)
- {
- //adds it into a vector
- votes.push_back(vote);
- }
- countVotes();
- return true;
- }
- //count each vote in a file
- void countyElectionResults::countVotes()
- {
- for(int i=0; i < votes.size(); i++)
- {
- switch(votes[i])
- {
- case 'f':
- case 'F':
- {
- fielditeVotes++;
- break;
- }
- case 'n':
- case 'N':
- {
- nullifierVotes++;
- break;
- }
- default:
- {
- otherVotes++;
- break;
- }
- }
- }
- }
- // Returns the county name
- string countyElectionResults::getCountyName()
- {
- return countyName;
- }
- // Returns the vector that contains all the votes that were loaded
- vector<char> countyElectionResults::getVotes()
- {
- return votes;
- }
- // Prints the votes that were loaded
- void countyElectionResults::showVotes()
- {
- for(int i=0; i < votes.size(); i++)
- {
- cout << votes[i] << endl;
- }
- }
- // Returns the total fieldite votes
- int countyElectionResults::getFielditeVotes()
- {
- return fielditeVotes;
- }
- // Returns the total third party votes
- int countyElectionResults::getOtherVotes()
- {
- return otherVotes;
- }
- // Returns the total nullifier votes
- int countyElectionResults::getNullifierVotes()
- {
- return nullifierVotes;
- }
- // Returns the total number of votes
- int countyElectionResults::getTotalVotes()
- {
- int totalVotes = 0;
- totalVotes = nullifierVotes + fielditeVotes + otherVotes;
- return totalVotes;
- }
- //shows the votes of each county listed in the program
- void stateElectionResults::showAllCountyResults()
- {
- int FTotal = 0;
- int NTotal = 0;
- int OTotal = 0;
- cout << "County Name" << setw(20) << "Fieldite Votes" << setw(20) << "Nullifier Votes" << setw(20) << "Other Votes" << endl;
- //for loop to go through each county in the vector to return the votes
- for(int i=0; i < stateResults.size();i++)
- {
- cout << stateResults[i].getCountyName() << setw(20) << stateResults[i].getFielditeVotes() << setw(20) << stateResults[i].getNullifierVotes() << setw(20) << stateResults[i].getOtherVotes() << endl;
- }
- //for loop to go through each county in the vector to return the totals
- for(int i=0; i < stateResults.size(); i++)
- {
- FTotal = FTotal + stateResults[i].getFielditeVotes();
- NTotal = NTotal + stateResults[i].getNullifierVotes();
- OTotal = OTotal + stateResults[i].getOtherVotes();
- }
- cout << "Total: " << setw(20) << FTotal << setw(20) << NTotal << setw(20) << OTotal << endl;
- }
- //searches a county a shows the results for that county
- void stateElectionResults::showCountyResults(string filename)
- {
- string selection;
- cout << "Please enter the county name: ";
- cin >> selection;
- for(int i=0; i < stateResults.size();i++)
- {
- //if there is an equal county name, then it shows the results
- if(stateResults[i].getCountyName() == selection)
- {
- cout << endl << stateResults[i].getCountyName() << setw(20) << stateResults[i].getFielditeVotes() << setw(20) << stateResults[i].getNullifierVotes() << setw(20) << stateResults[i].getOtherVotes() << endl;
- break;
- }
- }
- }
- //lists all of the counties in the program on the screen
- void stateElectionResults::listCounties()
- {
- cout << endl << endl;
- cout << "COUNTIES" << endl;
- for(int i=0; i < stateResults.size();i++)
- {
- cout << stateResults[i].getCountyName() << endl;
- }
- }
- //shows each actual vote for a specificed county
- void stateElectionResults::showCountyVotes(string userinput)
- {
- string selection;
- cout << "Please enter the county name: ";
- cin >> selection;
- for(int i=0; i < stateResults.size();i++)
- {
- if(stateResults[i].getCountyName() == selection)
- {
- //makes a temp vector to store the votes of the found county's votes
- vector<char> temp;
- temp = stateResults[i].getVotes();
- //prints those votes to the screen
- for(int i=0; i < temp.size();i++)
- {
- cout << temp[i] << endl;
- }
- return;
- }
- }
- }
- // (1) Asks the user for a file name, then (2) loads that file into
- // a countyElectionObject from that file and (3) places it into a vector
- void stateElectionResults::addCountyFile(){
- string filename;
- countyElectionResults county_results;
- cout << "Enter the county file to process: "; // 1.
- //allows for a space in the file name
- getline(cin, filename);
- if(!county_results.loadFile(filename)){ // 2.
- // loading file didn't work
- cout << "File not found: " << filename << endl << endl;
- return;
- }
- // loading file worked, so push the object onto the vector
- stateResults.push_back(county_results); // 3.
- return;
- }
- //function to get the menu choice of the user
- char get_menu_choice()
- {
- cout << endl << endl << endl;
- cout << "A: Add a county file" << endl;
- cout << "L: List counties" << endl;
- cout << "R: show all results" << endl;
- cout << "C: show single county result" << endl;
- cout << "V: verify single county votes" << endl;
- cout << "Q: quit" << endl << endl << endl << endl;
- cout << "Your choice: ";
- char selection;
- cin >> selection;
- //fixes the error of adding extra characters to the menu choice
- cin.ignore(500,'\n');
- return selection;
- }
- int main(){
- stateElectionResults S1;
- char selection;
- string filename;
- 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')
- {
- S1.addCountyFile();
- }
- if(selection == 'R' || selection == 'r')
- {
- S1.showAllCountyResults();
- }
- if(selection == 'C' || selection == 'c')
- {
- S1.showCountyResults(filename);
- }
- if(selection == 'L' || selection == 'l')
- {
- S1.listCounties();
- }
- if(selection == 'V' || selection == 'v')
- {
- S1.showCountyVotes(filename);
- }
- }while((selection != 'Q' && selection != 'q'));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement