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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
//CST116_Lab3_joetraver.ccp
#include <iostream>
#include <fstream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::setprecision;
using std::fixed;
using std::ios;
using std::ifstream;
using std::ofstream;
const int MAX = 100;
// Functions used
int ReadData(ifstream& inFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], float tfare[], float CPM[]);
void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], float tfare[], float CPM[],
int counter);
void PrintTotalsAndSummary(ofstream& outFile, int totalRecords);
void PassengerCount(ofstream& outFile, int psgr[], int counter, int &pplt);
void TotalPaid(ofstream& outFile, float tfare[], int counter, float &paid);
void AverageCostPerPerson(ofstream& outFile, float paid, int pplt, float ACM);
int main()
{
// All variables
int pick[MAX];
int drop[MAX];
int psgr[MAX];
float dist[MAX];
float fare[MAX];
float toll[MAX];
int counter = 0;
float tfare[MAX];
float CPM[MAX];
int pplt = 0;
float paid = 0;
float ACM = 0;
int record_counter(0);
ifstream inFile;
// Report file location
ofstream outFile("C:\\TEMP\\lab3_report.txt");
// Read file location
inFile.open("C:\\TEMP\\lab3_data.txt");
// Check to ensure read file has been opened
if (inFile.is_open())
{
// Establishes the amount of records present and defines the loop counters
// Places the values in each feild in arrays
record_counter = ReadData(inFile, pick, drop, psgr, dist, fare, toll, tfare, CPM);
inFile.close();
// Check to ensure report file is accessable
if (outFile.is_open())
{
// Writes values to report with calculations
WriteOutputFile(outFile, pick, drop, psgr, dist, fare, toll, tfare, CPM, record_counter);
PassengerCount(outFile, psgr, record_counter, pplt);
TotalPaid(outFile, tfare, record_counter, paid);
AverageCostPerPerson(outFile, paid, pplt, ACM);
PrintTotalsAndSummary(outFile, record_counter);
outFile.close();
}
// Failsafe Error message
else
{
cout << "Trouble Opening File";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
}
// Failsafe Error message
else
{
cout << "Trouble Opening File";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
return 0;
}
// Read and store data from file
int ReadData(ifstream& inFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], float tfare[], float CPM[])
{
int counter = 0;
inFile >> pick[counter] >> drop[counter] >> psgr[counter] >> dist[counter] >> fare[counter] >> toll[counter];
cout << setiosflags(ios::left)
<< "Pickup location" << resetiosflags(ios::left)
<< setw(24) << "Drop-off location" << resetiosflags(ios::left)
<< setw(15) << "# of ppl" << resetiosflags(ios::left)
<< setw(17) << "Distance" << resetiosflags(ios::left)
<< setw(11) << "Fare" << resetiosflags(ios::left)
<< setw(15) << "Toll" << resetiosflags(ios::left)
<< setw(16) << "Total Fare" << resetiosflags(ios::left)
<< setw(18) << "Cost Per Mile" << resetiosflags(ios::left)
<< endl << endl;
while (!inFile.eof())
{
tfare[counter] = fare[counter] + toll[counter];
if (dist[counter] == 0)
CPM[counter] = 0;
else
CPM[counter] = fare[counter] / dist[counter];
cout << fixed << setprecision(2) << setiosflags(ios::left) << setw(10)
<< pick[counter] << resetiosflags(ios::left)
<< setw(20) << drop[counter] << resetiosflags(ios::left)
<< setw(20) << psgr[counter] << resetiosflags(ios::left)
<< setw(19) << dist[counter] << resetiosflags(ios::left)
<< setw(14) << fare[counter] << resetiosflags(ios::left)
<< setw(14) << toll[counter] << resetiosflags(ios::left)
<< setw(14) << tfare[counter] << resetiosflags(ios::left)
<< setw(14) << CPM[counter] << resetiosflags(ios::left)
<< endl;
counter++;
inFile >> pick[counter] >> drop[counter] >> psgr[counter] >> dist[counter] >> fare[counter] >> toll[counter];
}
return counter;
}
// Writes the report from the given file
// Formats data for external file
void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], float tfare[], float CPM[],
int counter)
{
outFile << "\n\n\t\t\t\t\t\t\t Here is the Output File" << endl << endl;
outFile << setiosflags(ios::left)
<< "Pickup location" << resetiosflags(ios::left)
<< setw(24) << "Drop-off location" << resetiosflags(ios::left)
<< setw(15) << "# of ppl" << resetiosflags(ios::left)
<< setw(17) << "Distance" << resetiosflags(ios::left)
<< setw(11) << "Fare" << resetiosflags(ios::left)
<< setw(15) << "Toll" << resetiosflags(ios::left)
<< setw(16) << "Total Fare" << resetiosflags(ios::left)
<< setw(18) << "Cost Per Mile" << resetiosflags(ios::left)
<< endl << endl;
for (int i = 0; i < counter; i++)
{
outFile << fixed << setprecision(2) << setiosflags(ios::left) << setw(10)
<< pick[i] << resetiosflags(ios::left)
<< setw(20) << drop[i] << resetiosflags(ios::left)
<< setw(20) << psgr[i] << resetiosflags(ios::left)
<< setw(19) << dist[i] << resetiosflags(ios::left)
<< setw(14) << fare[i] << resetiosflags(ios::left)
<< setw(14) << toll[i] << resetiosflags(ios::left)
<< setw(14) << tfare[i] << resetiosflags(ios::left)
<< setw(14) << CPM[i] << resetiosflags(ios::left)
<< endl;
}
}
// Sums passengers transported
// Places value in report and command prompt
void PassengerCount(ofstream& outFile, int psgr[], int counter, int &pplt)
{
for (int i = 0; i < counter; i++)
{
pplt = pplt + psgr[i];
}
cout << endl << "\t\t\t\t\t\tTotal number of passengers transported: " << pplt << endl;
outFile << fixed << setprecision(2) << endl << "\t\t\t\t\t\tTotal number of passengers transported: " << pplt << endl;
}
// Sums the total fairs paid
// Places value in report and command prompt
void TotalPaid(ofstream& outFile, float tfare[], int counter, float &paid)
{
for (int i = 0; i < counter; i++)
{
paid = paid + tfare[i];
}
cout << fixed << setprecision(2) << "\t\t\t\t\t\tTotal fairs paid: $" << paid << endl;
outFile << fixed << setprecision(2) << "\t\t\t\t\t\tTotal fairs paid: $" << paid << endl;
}
// Average fair per person transported
// Places value in report and command prompt
void AverageCostPerPerson(ofstream& outFile, float paid, int pplt, float ACM)
{
ACM = paid / pplt;
cout << "\t\t\t\t\t\tAverage cost per person: $" << ACM << endl;
outFile << fixed << setprecision(2) << "\t\t\t\t\t\tAverage cost per person: $" << ACM << endl;
}
// Prints file foot note with entry count
void PrintTotalsAndSummary(ofstream& outFile, int totalRecords)
{
cout << "\n\n\t\t\t\t\t\t** Total Records: " << totalRecords << " **\n"
<< "\t\t\t\t\t\t\t The End \n";
outFile << "\n\n\t\t\t\t\t\t** Total Records: " << totalRecords << " **\n"
<< "\t\t\t\t\t\t\t The End \n";
}
|