Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string> //allows you to create variables of the datatype string using namespace std
- #include <iostream> //p183 in book
- #include <fstream> //allows reading and writing to files
- #include <iomanip> //allows you to format output
- using namespace std;
- int main()
- {
- ifstream infile;
- ofstream outfile;
- char vote;
- int district;
- int overallTotal=0;
- int yesTotal=0;
- int noTotal=0;
- int d1Yesvotes=0;
- int d1Novotes=0;
- int d2Yesvotes=0;
- int d2Novotes=0;
- int d3Yesvotes=0;
- int d3Novotes=0;
- //open file
- infile.open("votes.dat");
- //online help start
- string line;
- if (!infile.is_open()) {
- cout << "Error opening file" << endl;
- return 0;}
- while (getline(infile, line)) {
- cout << line << endl;
- if (line.size() < 3)
- continue;
- district=line[0];
- vote=line[2];
- //online help end
- //if statement for 'yes' and 'no' votes, containing nested if statements
- if (district=='1')
- {
- if(vote=='Y')
- {
- yesTotal++;
- d1Yesvotes++;
- overallTotal++; }
- else if(vote=='N')
- {
- noTotal++;
- d1Novotes++;
- overallTotal++; }
- }
- else if (district=='2')
- {
- if(vote=='Y')
- { yesTotal++;;
- d2Yesvotes++;
- overallTotal++;}
- else if(vote=='N')
- { noTotal++;
- d2Novotes++;
- overallTotal++;}
- }
- else if (district=='3')
- {
- if(vote=='Y')
- { yesTotal++;
- d3Yesvotes++;
- overallTotal++;}
- else if(vote=='N')
- { noTotal=noTotal+1;
- d3Novotes++;
- overallTotal++;
- }
- }
- }
- outfile.open("votingresults.txt");
- outfile<<endl<<"Number of Overall votes: "<<overallTotal;
- outfile<<endl;
- outfile<<endl<<"Number of Yes votes: "<<yesTotal;
- outfile<<endl<<"Number of No votes: "<<noTotal;
- outfile<<endl;
- outfile<<endl<<"Number of District 1 Yes Votes: "<<d1Yesvotes;
- outfile<<endl<<"Number of District 1 No Votes: "<<d1Novotes;
- outfile<<endl;
- outfile<<endl<<"Number of District 2 Yes Votes: "<<d2Yesvotes;
- outfile<<endl<<"Number of District 2 No Votes: "<<d2Novotes;
- outfile<<endl;
- outfile<<endl<<"Number of District 3 Yes Votes: "<<d3Yesvotes;
- outfile<<endl<<"Number of District 3 No Votes: "<<d3Novotes;
- //close files
- infile.close();
- outfile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement