Advertisement
teknique

Untitled

Dec 10th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.40 KB | None | 0 0
  1.  //
  2.  // Project 3
  3.  // Name: Jay Mellick
  4.  // Netid: jtm79
  5.  // COSC 051
  6.  // Section: 2
  7.  // In accordance with the class policies and Georgetown's Honor Code,
  8.  // I certify that I have neither given nor received any assistance
  9.  // on this project with the exceptions of the lecture notes and those
  10.  // items noted below.
  11.  //
  12.  // Any help items:
  13.  //
  14.  // Description: This program lets the user perform different tasks with county election files.
  15.  //
  16.  
  17. #include <iostream>
  18. #include <string>
  19. #include <iomanip>
  20. #include <fstream>
  21. #include <sstream>
  22. #include <vector>
  23. #include <algorithm>
  24. using namespace std;
  25.  
  26. //First function to print the results of the county election file
  27. 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)
  28. {
  29.     cout << endl;
  30.     cout << endl;
  31.     cout << endl;
  32.     cout << "County" << setw(20) << "Nullifier" << setw(20) << "Fieldite" << setw(20) << "Third Party" << endl;
  33.     cout << "----------------------------------------------------------------------" << endl;
  34.     //CountyNameVector is a vector which holds all the county names of the files that have been loaded
  35.     for(int i = 0; i < countyNameVector.size(); i++)
  36.     {
  37.         cout << countyNameVector[i] << setw(20) << countyNCount[i] << setw(20) << countyFCount[i] << setw(20) << countyOCount[i] << endl;
  38.         //This is totalling up the tallys of votes for each party
  39.         NTotal = NTotal + countyNCount[i];
  40.         FTotal = FTotal + countyFCount[i];
  41.         OTotal = OTotal + countyOCount[i];
  42.     }
  43.     cout << "-----------------------------------------------------------------------" << endl;
  44.     cout << "Total" << setw(20) << NTotal << setw(20) << FTotal << setw(20) << OTotal << endl << endl << endl << endl << endl;
  45. }
  46.  
  47. //Function that searches for a specific county name in the loaded files
  48. void search_county(const std::vector<string> & countyNameVector, const std::vector<int> & countyNCount, const vector<int> & countyFCount, const std::vector<int> & countyOCount)
  49. {
  50.     string searchname;
  51.     cout << "Enter the county file to search for: ";
  52.     cin >> searchname;
  53.     for(int i = 0; i < countyNameVector.size(); i++)
  54.     {
  55.         if(searchname == countyNameVector[i])
  56.         {
  57.             cout << endl;
  58.             cout << endl;
  59.             cout << "County" << setw(20) << "Nullifier" << setw(20) << "Fieldite" << setw(20) << "Third Party" << endl;
  60.             cout << "-----------------------------------------------------------------------" << endl;
  61.             cout << countyNameVector[i] << setw(20) << countyNCount[i] << setw(20) << countyFCount[i] << setw(20) << countyOCount[i] << endl;
  62.             cout << "-----------------------------------------------------------------------" << endl;
  63.             cout << endl;
  64.             cout << endl;
  65.             return;
  66.         }
  67.     }
  68.     cout << endl;
  69.     //if no county name is found, then it returns a message
  70.     cout << "No county was found!" << endl << endl;
  71. }
  72.  
  73. //Function that lets the user add a county election file to the program
  74. 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)
  75. {
  76.     char vote;
  77.     string filename;
  78.     string countyName;
  79.     NCount = 0;
  80.     FCount = 0;
  81.     OCount = 0;
  82.    
  83.     cout << "Enter the county file to process: ";
  84.     cin >> filename;
  85.     std::ifstream input((filename).c_str());
  86.     if(input.is_open())
  87.     {
  88.         input >> countyName;
  89.         //adds the county name to the vector of names
  90.         countyNameVector.push_back(countyName);
  91.         while(input >> vote)
  92.         {
  93.             if(vote == 'N' || vote == 'n')
  94.             {
  95.                 NCount = NCount + 1;
  96.             }
  97.             else if(vote == 'F' || vote == 'f')
  98.             {
  99.                 FCount = FCount + 1;
  100.             }
  101.             else
  102.             {
  103.                 OCount = OCount + 1;
  104.             }
  105.         }
  106.         //adds the count of party votes for the current file to a vector
  107.         countyNCount.push_back(NCount);
  108.         countyFCount.push_back(FCount);
  109.         countyOCount.push_back(OCount);
  110.     }
  111.     else
  112.     {
  113.     cout << endl;
  114.     cout << "File not found!" << endl;
  115.     cout << endl;
  116.     }
  117. }
  118.  
  119. //function to get which choice the user is selecting from the menu
  120. char get_menu_choice()
  121. {
  122.     cout << "Add a county election file         A" << endl;
  123.     cout << "Show election totals on screen     P" << endl;
  124.     cout << "Search for county results          S" << endl;
  125.     cout << "Exit the program                   Q" << endl;
  126.     cout << "Please enter your choice: ";
  127.     char selection;
  128.     cin >> selection;
  129.     return selection;
  130. }
  131.  
  132. int main()
  133. {
  134.     vector <string> countyNameVector;
  135.     vector <int> countyNCount;
  136.     vector <int> countyFCount;
  137.     vector <int> countyOCount;
  138.     int NCount = 0;
  139.     int FCount = 0;
  140.     int OCount = 0;
  141.     int NTotal = 0;
  142.     int FTotal = 0;
  143.     int OTotal = 0;
  144.     char selection;
  145.    
  146.     do
  147.     {
  148.         //this sets the user choice to a variable, which is used for our if statement
  149.         selection = get_menu_choice();
  150.        
  151.     if(selection == 'a' || selection == 'A')
  152.     {
  153.         add_county_election_file(countyNameVector, countyNCount, countyFCount, countyOCount, NCount, FCount, OCount);
  154.     }
  155.        
  156.     if(selection == 'p' || selection == 'P')
  157.     {
  158.         print_results(countyNameVector, countyNCount, countyFCount, countyOCount, NTotal, FTotal, OTotal);
  159.        
  160.     }
  161.     if(selection == 'S' || selection == 's')
  162.     {
  163.         search_county(countyNameVector, countyNCount, countyFCount, countyOCount);
  164.     }
  165.        
  166.        
  167.     }while((selection != 'Q' && selection != 'q'));
  168.     return 0;
  169.    
  170.    
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement