In: Computer Science
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