1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include<iostream>
#include <iomanip>
#include <fstream>
// you are not using any of the below. so take them out.
//#include <cstdlib>
//#include<ctime>
//#include<string>
//#include<cstring>
//#include<cmath>
using namespace std;
const double ZONE_A = 75.00;
const double ZONE_B = 55.00;
const double ZONE_C = 30.00;
const int SEATS = 100;
struct TicketHolder {
string holderFName;
string holderLName;
char zone;
double charge;
};
int loadData(TicketHolder audience[]);
void addCharge(TicketHolder audience[], int numPeople);
double totalCharge(TicketHolder audience[], int numPeople);
void outputData(TicketHolder audience[], int numPeople, double someTotal);
int main() {
int numPeople = 0;
double someCharge = 0;
TicketHolder audience[SEATS];
numPeople = loadData (audience);
addCharge (audience, numPeople);
someCharge = totalCharge (audience, numPeople);
outputData (audience, numPeople, someCharge);
// do not do this it is a windows thing and it does not work on all comptuers.
// see the top sticky thread of this forum for more info.
//system("pause");
return 0;
}
int loadData(TicketHolder audience[]) {
TicketHolder inputStruct;
string inputFile;
ifstream inFile;
int i = 0;
cout << "Please enter the file name of the audience names and their zones " << endl;
cin >> inputFile;
inFile.open (inputFile);
if (inFile) {
// we have a file
while (inFile.peek () != EOF) {
inFile >> inputStruct.holderFName;
cin.ignore ();
inFile >> inputStruct.holderLName;
cin.ignore ();
inFile >> inputStruct.zone;
cin.ignore ();
audience[i] = inputStruct;
i++;
if (i == 99) {
cout << " Out of Seats " << endl;
break;
}
}
// you are returning before you close your file.
// return i;
// inFile.close();
// do this instead
inFile.close ();
return i;
} else {
// we do not have a file
cout << "No file found, check your inputs!" << endl;
}
}
void addCharge(TicketHolder audience[], int numPeople) {
//TicketHolder audience;
for (int i = 0; i < numPeople; i++) {
if (audience[i].zone == 'A')
audience[i].charge = ZONE_A;
else if (audience[i].zone == 'B')
audience[i].charge = ZONE_B;
else if (audience[i].zone == 'C')
audience[i].charge = ZONE_C;
}
}
double totalCharge(TicketHolder audience[], int numPeople) {
double total = 0;
for (int i = 0; i < numPeople; i++) {
total += audience[i].charge;
}
return total;
}
void outputData(TicketHolder audience[], int numPeople, double someTotal) {
TicketHolder output;
ofstream outFile;
string outputFile;
cout << " What do you want the output file to be called? " << endl;
cin >> outputFile;
outFile.open (outputFile);
for (int i = 0; i < numPeople; i++) {
outFile << output.holderLName;
outFile << output.holderFName;
outFile << output.zone;
outFile << someTotal;
}
outFile.close ();
}
|