Question
Your job is to write a program that will help us keep track of the donations to these candidates….
Help with C++ Code (using visual studio)
preferred Typed
Result:
Your job is to write a program that will help us keep track of the donations to these candidates. You will assume that the program is written for this election where Anders and Valerie are running. It is OK to hard code these into your program. Don't worry about case sensitivity of names Here are the features of the program Feature #1: Contribute to a Campaign [60 points] When someone wants to make a contribution we use this feature to enter the contribution information to the system. Program asks for which candidate the contribution is, and the amount of the contribution, along with the name of the person making the contribution To make things easier for you – You may assume that the name of the donor is just the last name. No spaces allowed in the last name and assume that no two donors will ever have the same last name.:-) According to US laws, no individual is allowed to make political contributions exceeding $2700 per election. The program should not accept the contribution if the total amount of contribution of that individual (go by last name) exceeds $2700 [50 points] The candidate that will be entered by the user will always be either Anders or Valerie -you do not need to error check for this The program supports up to 1000 donations. Feature #2: Report per Candidate a) Displays how many contributions are in the system for each candidate. [30 points] b) The total amount contributed for each candidate. [30 points] c) The average contribution amount for each candidate. This is basically the number you found for a) divided by the number you reported for b) for each candidate. [30 points] File I/O [60 points]: Make sure that you have file l/O in place so we do not loose information when we close the program Classes [40 points]: Make sure that you are using a class representing a donation
Election Contributions v1.0 Anders vs. Valerie 1- Contribute to a Campaign 2- Report per Candidate 0- Exit Make a choice (1-3): 1 Candidate (Anders or Valerie): Anders Donation Amount: 1300 Your Name: Jackson Election Contributions v1.0 Anders vs. Valerie 1- Contribute to a Campaign 2- Report per Candidate 0- Exit Make a choice (1-3): 1 Candidate (Anders or Valerie): Anders Donation Amount: 1500 Your Name: Jackson You cannot exceed $2700 per election Election Contributions v1.0 Anders vs. Valerie 1- Contribute to a Campaign 2- Report per Candidate 0- Exit Make a choice (1-3): 2 Anders Total: $1300 # of Donations: 1 lvg. Amount: $1300 Valerie Total: 0 # of Donations: 0 lvg. Amount $0 Election Contributions v1.0 Anders vs. Valerie 1- Contribute to a Campaign 2- Report per Candidate 0- Exit Make a choice (1-3): 1 Candidate (Anders or Valerie): Valerie Donation Amount: 1500 Your Name: Brown Election Contributions v1.0 Anders vs. Valerie 1- Contribute to a Campaign 2- Report per Candidate 0- Exit Make a choice (1-3): 2 AndersTotal of Donations: 1 Avg. Amount: $1300 alerie Total of Donations: 1
Solutions
Expert Solution
Please let me know if you have any doubts or you want me to
modify the answer. And if you find this answer useful then don't
forget to rate my answer as thumps up. Thank you! 🙂
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Donation {
public:
string name;
double amount;
string candidate;
};
void displayMessage();
void readFile(Donation donors[], int &count);
void writeFile(Donation donors[], int count);
void addContribution(Donation *donors, int &count, int
&countAnders, int &countValerie, double
&totalAnders,
double &totalValerie);
void eachCandidateReport(Donation *donors, int count, int
countAnders, int countValerie, double totalAnders,
double totalValerie);
int main() {
int choice = 0;
const int SIZE = 1000;
Donation donors[SIZE];
int count = 0;
int countAnders = 0;
int countValerie = 0;
double totalAnders = 0;
double totalValerie = 0;
readFile(donors, count);
do {
displayMessage();
cin >> choice;
if (choice == 1) {
addContribution(donors, count, countAnders, countValerie,
totalAnders, totalValerie);
}
else if (choice == 2) {
eachCandidateReport(donors, count, countAnders, countValerie,
totalAnders, totalValerie);
}
} while (choice != 0);
writeFile(donors, count);
system("PAUSE");
return 0;
}
//function definitions
void displayMessage() {
cout << "===========================\n"
<< "Election Contributions
v1.0\n"
<< "
Anders vs. Valerie \n"
<<
"===========================\n"
<< "1 – Contribute to
Campaign\n"
<< "2 – Report per
Candidate\n"
<< "0 – Exit\n"
<< "Choose one: ";
}
void readFile(Donation donors[],int &count) {
ifstream
fileIn("/Users/swapnil/CLionProjects/ElectionContributions
/input.txt");
if (!fileIn) {
cout << "File has failed to
open.\n";
system("PAUSE");
exit(EXIT_FAILURE);
}
while (!fileIn.eof()) {
fileIn >>
donors[count].name;
fileIn >>
donors[count].amount;
fileIn >>
donors[count].candidate;
count++;
}
}
void writeFile(Donation donors[], int count) {
ofstream
fileOut("/Users/swapnil/CLionProjects/ElectionContributions
/output.txt");
for (int i = 0; i < count; i++) {
fileOut << donors[i].name
<< " " << donors[i].amount << " " <<
donors[i].candidate;
if (i < count – 1) {
fileOut <<
endl;
}
}
fileOut.close();
}
void addContribution(Donation *donors, int &count, int
&countAnders, int &countValerie, double
&totalAnders,
double &totalValerie) {
for (int i = 0; i < count; i++) {
if (donors[i].candidate ==
"Anders") {
totalAnders +=
donors[i].amount;
}
if (donors[i].candidate ==
"Valerie") {
totalValerie +=
donors[i].amount;
}
}
cout << "What is your last name?\n";
cin >> donors[count].name;
for (int i = 0; i < count; i++) {
double total = 0;
if (donors[count].name ==
donors[i].name) {
total +=
donors[i].amount;
}
if (total > 2700) {
cout <<
"You are not allowed to donate more. Your contribution will not be
accepted.\n";
return;
}
}
cout << "How much do you want to
donate?\n";
cin >> donors[count].amount;
cout << "Which candidate are you donating to?
(Anders OR Valerie)\n";
cin >> donors[count].candidate;
if (donors[count].candidate == "Anders") {
countAnders++;
}
if (donors[count].candidate == "Valerie") {
countValerie++;
}
count++;
}
void eachCandidateReport(Donation *donors, int count, int
countAnders, int countValerie, double totalAnders,
double totalValerie) {
double avg = 0;
avg = totalAnders / countAnders;
cout << "The number of contributions is: "
<< countAnders << endl;
cout << "The total amount collected for Anders
is: " << totalAnders << endl;
cout << "The average amount collected for Anders
is: " << avg << endl;
avg = 0;
avg = totalValerie / countValerie;
cout << "The number of contributions is: "
<< countValerie << endl;
cout << "The total amount collected for Valerie
is: " << totalValerie << endl;
cout << "The average amount collected for
Valerie is: " << avg << endl;
}