Advertisement
teknique

Untitled

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