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
|
// Lab3
//
// CST116
//
// Taylor Rogers
//
#include <iostream>
#include <fstream>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::setw;
using std::ios;
using std::string;
using std::ifstream;
using std::ofstream;
const int MAX = 50;
int psgrsum = 0;
float totalsum = 0;
int ReadData(ifstream& inFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[]);
void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], float total[], float cmp[], int counter);
void PrintTotalsAndSummary(ofstream& out, int totalRecords, int totalPassengers, float totalSum);
int main()
{
// MAX set to 50 since big.txt is <50
int pick[MAX];
int drop[MAX];
int psgr[MAX];
float dist[MAX];
float fare[MAX];
float toll[MAX];
float total[MAX];
float cpm[MAX];
int record_counter(0);
string fnameinput;
// Filename string from user input
cout << "Enter the file name you wish to read data from: ";
cin >> fnameinput;
cout << endl;
ifstream inFile;
ofstream outFile("CST116-Lab3_Report-Rogers.txt");
inFile.open(fnameinput);
if (inFile.is_open())
{
record_counter = ReadData(inFile, pick, drop, psgr, dist, fare, toll);
inFile.close();
if (outFile.is_open())
{
WriteOutputFile(outFile, pick, drop, psgr, dist, fare, toll, total, cpm, record_counter);
PrintTotalsAndSummary(outFile, record_counter, psgrsum, totalsum);
outFile.close();
}
else
{
cout << "Trouble Opening File" << endl;
cout << "Exiting..." << endl;
}
}
else
{
cout << "Trouble Opening File" << endl;
cout << "Exiting..." << endl;
}
return 0;
}
// Reads data from the user specified file
int ReadData(ifstream& inFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[])
{
int counter = 0;
inFile >> pick[counter] >> drop[counter] >> psgr[counter] >> dist[counter] >> fare[counter] >> toll[counter]; // Priming Read
// Column headers
cout << "*****Data Read From File*****" << endl;
cout << endl;
cout << std::left
<< setw(15) << "Pickup"
<< setw(15) << "Dropoff"
<< setw(15) << "Passengers"
<< setw(15) << "Distance"
<< setw(15) << "Fare"
<< setw(15) << "Toll"
<< endl;
while (!inFile.eof())
{
cout << setiosflags(ios::left)
<< setw(15) << pick[counter]
<< setw(15) << drop[counter]
<< setw(15) << psgr[counter]
<< setw(15) << dist[counter]
<< setw(15) << fare[counter]
<< setw(15) << toll[counter]
<< endl;
counter++;
inFile >> pick[counter] >> drop[counter] >> psgr[counter] >> dist[counter] >> fare[counter] >> toll[counter];
}
return counter;
}
// Writes data to the output file Lab3_Report.txt. This function includes a "total" and "cost per mile" column which it calculates using the file data. The passenger and cost totals are summed.
void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], float total[], float cpm[],
int counter)
{
// Column headers
outFile << "Here is the Output File" << endl;
outFile << endl;
outFile << std::left
<< setw(15) << "Pickup"
<< setw(15) << "Dropoff"
<< setw(15) << "Passengers"
<< setw(15) << "Distance"
<< setw(15) << "Fare"
<< setw(15) << "Toll"
<< setw(15) << "Total"
<< setw(15) << "Cost per Mile"
<< endl;
cout << endl;
cout << "*****Here is the Output File*****" << endl;
cout << endl;
cout << std::left
<< setw(15) << "Pickup"
<< setw(15) << "Dropoff"
<< setw(15) << "Passengers"
<< setw(15) << "Distance"
<< setw(15) << "Fare"
<< setw(15) << "Toll"
<< setw(15) << "Total"
<< setw(15) << "Cost per Mile"
<< endl;
for (int r = 0; r <= counter - 1; r++)
{
// New columns for total cost and cost per mile
total[r] = fare[r] + toll[r];
cpm[r] = total[r] / dist[r];
// Summing
psgrsum += psgr[r];
totalsum += total[r];
// Transcribing data from file and adding total and cmp columns
outFile << std::left
<< setw(15) << pick[r]
<< setw(15) << drop[r]
<< setw(15) << psgr[r]
<< setw(15) << dist[r]
<< setw(15) << fare[r]
<< setw(15) << toll[r]
<< setw(15) << total[r]
<< setw(15) << cpm[r]
<< endl;
cout << std::left
<< setw(15) << pick[r]
<< setw(15) << drop[r]
<< setw(15) << psgr[r]
<< setw(15) << dist[r]
<< setw(15) << fare[r]
<< setw(15) << toll[r]
<< setw(15) << total[r]
<< setw(15) << cpm[r]
<< endl;
}
}
// This function outputs the total number of entries from the data file, along with some statistics derived from the provided data.
void PrintTotalsAndSummary(ofstream& outFile, int totalRecords, int totalPassengers, float totalSum)
{
float avgcost = 0;
avgcost = totalSum / totalPassengers;
// To screen
cout << endl;
cout << "Total Records: " << totalRecords << endl;
cout << "Total Passengers: " << totalPassengers << endl;
cout << "Total Cost: " << totalSum << endl;
cout << "Average Cost: " << avgcost << endl;
// To file
outFile << endl;
outFile << "Total Records: " << totalRecords << endl;
outFile << "Total Passengers: " << totalPassengers << endl;
outFile << "Total Cost: " << totalSum << endl;
outFile << "Average Cost: " << avgcost << endl;
}
|