Question

In: Computer Science

Add File I/O to the voting program below #include<iostream> using namespace std; int main() {int choice;...

Add File I/O to the voting program below

#include<iostream>
using namespace std;
int main()
{int choice;
int biden = 0 , trump = 0 , bugs = 0 ;
int vc = 0 ;
do {
cout<<"\n\n\nEVOTE\n-----"
<<"\n1.Joe Biden"
<<"\n2.Donald Trump"
<<"\n3.Bugs Bunny"
// 4. Print current tally [hidden admin option]
// 5. Print audit trail [hidden admin option]
// 6. mess with the vote [hidden hacker option] E.C.
// 7. END THE ELECTION
<<"\n\n Your selection? ";
cin >> choice ;   
if(choice==1) {biden++ ; vc++ ; cout<<"\nThanks for voting!"; }
if(choice==2) {trump++ ;vc++ ; cout<<"\nThanks for voting!"; }
if(choice==3) { bugs++ ; vc++ ; cout<<"\nThanks for voting!"; }
if((choice<1)||(choice>7))cout<<"\nPlease enter 1-3";
// write what happened to FILES
// Tally.txt (overwrite) biden x trump x bugs x
// auditTrail.txt ios::app (everything typed) 1 2 3 3 56 -12 4
} while(choice != 7 ) ;
  
cout<<"\n\nRESULTS:\n--------"
<<"\nTotal Votes Cast This Election: "<< vc
<<"\nJoe Biden: "<< biden
<<"\nDonald Trump: "<< trump
<<"\nBugs Bunny: "<<bugs;
  
cout<<"\n\n";
system("pause"); // leave this out if using newer DEV C++
return 0;
}

Solutions

Expert Solution

We have used filestream in c++ to write data to files. I have completed the steps asked in comment form and final code is given below.

#include <iostream>
// To use files 
#include<fstream>
using namespace std;
int main()
{
        int choice;
        int biden = 0, trump = 0, bugs = 0;
        int vc = 0;
//      declare file_stream and open auditTrail.txt in append mode
        fstream auditTrail_file;
        auditTrail_file.open("auditTrail.txt", ios::out | ios::app | ios::in);
        fstream tally_file;
        do {
                cout << "\n\n\nEVOTE\n-----" <<
                        "\n1.Joe Biden" <<
                        "\n2.Donald Trump" <<
                        "\n3.Bugs Bunny"
                        // 4. Print current tally[hidden admin option]
                        // 5. Print audit trail[hidden admin option]
                        // 6. mess with the vote[hidden hacker option] E.C.
                        // 7. END THE ELECTION
                        <<
                        "\n\n Your selection? ";
                cin >> choice;
                if (choice == 1)
                {
                        biden++;
                        vc++;
                        cout << "\nThanks for voting!";
                }

                if (choice == 2)
                {
                        trump++;
                        vc++;
                        cout << "\nThanks for voting!";
                }

                if (choice == 3)
                {
                        bugs++;
                        vc++;
                        cout << "\nThanks for voting!";
                }
        
                if ((choice < 1) || (choice > 7)) cout << "\nPlease enter 1-3";
                
                
                // write what happened to FILES
                // Tally.txt (overwrite) biden x trump x bugs x
        //open in truncate mode everytime to overwrite previous content
                tally_file.open("Tally.txt", ios::out | ios::trunc | ios::in);
                tally_file<<biden<<" "<<trump<<" "<<bugs;
                tally_file.close();
                // auditTrail.txt ios::app (everything typed) 1 2 3 3 56 -12 4
        //append user input to auditTrail_file 
                auditTrail_file<<choice<<" ";
        } while (choice != 7);

        cout << "\n\nRESULTS:\n--------" <<
                "\nTotal Votes Cast This Election: " << vc <<
                "\nJoe Biden: " << biden <<
                "\nDonald Trump: " << trump <<
                "\nBugs Bunny: " << bugs;

        cout << "\n\n";
        system("pause");        // leave this out if using newer DEV C++
        return 0;
}

Sample Output:

Tally.txt after it : 2 3 0

auditTrail.txt: 1 2 2 2 1 7


Related Solutions

Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I - Declaring a five by five array /* II - Read data from data.txt and use them to create the matrix in the previous step*/    // III - Count and print the number of even integers in the matrix. /* IV - Calculate and print the sum of all integers in the columns with an even index value. Please note the column index begins...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
Question 1 Given the program below, what are the errors. #include <iostream> using namespace std; int...
Question 1 Given the program below, what are the errors. #include <iostream> using namespace std; int main() { int ind_square(int &); int x, *p; x=15; p = &x; ind_square(*p); } int ind_square(int &p) { *p = *p **p; } Question 1 options: C. No return for non-void function A. Indirection for the variables in ind_square requires pointer operand A and B B. Invalided use of address (&) symbol as a parameter for ind_squared A and C Question 2 Which statement...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha = 'A'; for(int i = 0; i < 13; i++){ for(int j = 0; j < 2; j++){ cout << alpha; alpha++; } } cout << endl; return 0; }
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
How do i make this program accept strings? #include <iostream> #include <algorithm> using namespace std; int...
How do i make this program accept strings? #include <iostream> #include <algorithm> using namespace std; int binaryToOctal(char digits[], int a) { int number; if (digits[0] == '0') { if (digits[1] == '1') if (digits[2] == '0') return 2; //found "010" else return 3; //found "011" else if (digits[1] == '0') if (digits[2] == '1') return 1; // found "001" else return 0; //found "000" } else { if (digits[0] == '1') if (digits[1] == '1') if (digits[2] == '0') return...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT