Advertisement
riggnaros

Voting C++ code

Sep 20th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <string>  //allows you to create variables of the datatype string using namespace std
  2. #include <iostream>  //p183 in book
  3. #include <fstream>  //allows reading and writing to files
  4. #include <iomanip>  //allows you to format output
  5.  
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main()
  11.  
  12. {
  13.  
  14. ifstream infile;
  15. ofstream outfile;
  16. char vote;
  17. int district;
  18. int overallTotal=0;
  19. int yesTotal=0;
  20. int noTotal=0;
  21. int d1Yesvotes=0;
  22. int d1Novotes=0;
  23. int d2Yesvotes=0;
  24. int d2Novotes=0;
  25. int d3Yesvotes=0;
  26. int d3Novotes=0;
  27.  
  28.  
  29. //open file
  30. infile.open("votes.dat");
  31.  
  32.    
  33. //online help start
  34. string line;
  35. if (!infile.is_open()) {
  36.   cout << "Error opening file" << endl;
  37.   return 0;}
  38.    
  39.     while (getline(infile, line)) {
  40.   cout << line << endl;
  41.   if (line.size() < 3)
  42.     continue;
  43.   district=line[0];
  44.   vote=line[2];
  45.    
  46. //online help end
  47.    
  48.    
  49.    
  50. //if statement for 'yes' and 'no' votes, containing nested if statements
  51.    
  52.  
  53.    
  54. if (district=='1')
  55.     {        
  56.         if(vote=='Y')
  57.         {
  58.             yesTotal++;
  59.             d1Yesvotes++;
  60.             overallTotal++; }
  61.    
  62.     else if(vote=='N')
  63.     {
  64.         noTotal++;
  65.             d1Novotes++;
  66.             overallTotal++; }
  67.      }
  68.    
  69.     else if (district=='2')
  70.     {        
  71.         if(vote=='Y')
  72.         { yesTotal++;;
  73.             d2Yesvotes++;
  74.             overallTotal++;}
  75.    
  76.     else if(vote=='N')
  77.     { noTotal++;
  78.             d2Novotes++;
  79.             overallTotal++;}
  80.        }
  81.       else if (district=='3')
  82.     {        
  83.         if(vote=='Y')
  84.         { yesTotal++;
  85.             d3Yesvotes++;
  86.             overallTotal++;}
  87.    
  88.     else if(vote=='N')
  89.     { noTotal=noTotal+1;
  90.             d3Novotes++;
  91.             overallTotal++;
  92.             }
  93.    
  94.         }
  95.     }
  96.    
  97. outfile.open("votingresults.txt");
  98. outfile<<endl<<"Number of Overall votes: "<<overallTotal;
  99.     outfile<<endl;
  100. outfile<<endl<<"Number of Yes votes: "<<yesTotal;
  101. outfile<<endl<<"Number of No votes: "<<noTotal;
  102.     outfile<<endl;
  103. outfile<<endl<<"Number of District 1 Yes Votes: "<<d1Yesvotes;
  104.     outfile<<endl<<"Number of District 1 No Votes: "<<d1Novotes;
  105.     outfile<<endl;
  106. outfile<<endl<<"Number of District 2 Yes Votes: "<<d2Yesvotes;
  107.     outfile<<endl<<"Number of District 2 No Votes: "<<d2Novotes;
  108.     outfile<<endl;
  109. outfile<<endl<<"Number of District 3 Yes Votes: "<<d3Yesvotes;
  110.     outfile<<endl<<"Number of District 3 No Votes: "<<d3Novotes;
  111.    
  112.    
  113.    
  114.    
  115. //close files
  116.  
  117. infile.close();
  118.     outfile.close();
  119.  
  120.  
  121. return 0;
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement