blob: 01c3eb2be49d4ff124fbae9f2305cd4a57b8f3a4 (
plain) (
blame)
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
|
// BlankConsoleLab.cpp : This file contains the 'main' function. Program execution begins and ends there.
// CST116 Aaron Hill
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int pickUp[50];
int dropOff[50];
int passengerCount[50];
float distanceTravelled[50];
float fareAmount[50];
float tollAmount[50];
float costPerMile[50];
double totalFare[50];
int numEntries;
int ReadData(ifstream& file) {
// Takes a file input, reads the arrays in the file, and puts them in the corresponding array in the program.
// t is the number of entries found. Starts at -1 because the loop begins with an increment.
int t = -1;
while (!file.eof()) {
t++;
file >> pickUp[t] >> dropOff[t] >> passengerCount[t] >> distanceTravelled[t] >> fareAmount[t] >> tollAmount[t];
// Below are the calculations for the total fare and cost per mile.
totalFare[t] = fareAmount[t] + tollAmount[t];
if (distanceTravelled[t] != 0) {
costPerMile[t] = fareAmount[t] / distanceTravelled[t];
}
else costPerMile[t] = 0; // Special case for 0 miles ; prevents divide by 0 error.
}
t++; // Increment outside of the loop. Arrays begin at 0, but the returning value is the total number of objects, hence +1 at the end.
return t;
}
void GenerateTotals(int numEntries) {
// Loops through the arrays times the amount of entries found in the file. (This value is passed in.)
// Two local variables are initialized at 0 and each value from the arrays is added to the appropriate variable.
int totalPassengers = 0;
double totalPaid = 0;
for (int i = 0; i <= numEntries; i++) {
totalPassengers += passengerCount[i];
totalPaid += totalFare[i];
}
// Just printing the basic statistics.
cout << "TOTAL PASSENGERS: " << totalPassengers << endl;
cout << "TOTAL PAID: $" << totalPaid << endl;
cout << "AVG COST PER PERSON: $" << totalPaid / totalPassengers << endl;
cout << "TOTAL TRIPS: " << numEntries << endl;
}
int main()
{
// Some initialization
ifstream inFile;
string fileName;
cout << fixed << setprecision(2);
char choice = 'Y';
// Body contained within a loop to allow the user to enter another file.
while (choice == 'Y') {
// Until the user enters a valid file name, the program will not continue.
while (!inFile.is_open()) {
cout << "Please enter your data file name with the .txt extension:" << endl;
cin >> fileName;
inFile.open(fileName);
if (inFile.is_open()) {
cout << "\nOpened " << fileName << endl;
}
else {
cout << "\nFailed to open " << fileName << endl;
}
cout << endl;
}
// Fetching the # of entries and setting the values used in the program through ReadData().
int numEntries = ReadData(inFile);
// Printing basic statistics
GenerateTotals(numEntries);
// Asking the user if they want to show a table of the file they read in
choice = 'A';
while (choice != 'Y' && choice != 'N') {
cout << "\nWould you like to display a table? Y/N" << endl;
cin >> choice;
}
if (choice == 'Y') {
// Table headers
cout << left << setw(10) << "\nENTRY" << setw(10) << "PICKUP"
<< setw(10) << "DROPOFF" << setw(10) << "#PASS"
<< setw(10) << "DIST" << setw(10) << "FARE$"
<< setw(10) << "TOLL$" << setw(10) << "TOTAL$"
<< setw(10) << "$/MILE" << endl;
// Table values
for (int i = 0; i < numEntries; i++) {
cout << left << setw(10) << i + 1 << setw(10) << pickUp[i]
<< setw(10) << dropOff[i] << setw(10) << passengerCount[i]
<< setw(10) << distanceTravelled[i] << setw(10) << fareAmount[i]
<< setw(10) << tollAmount[i] << setw(10) << totalFare[i]
<< setw(10) << costPerMile[i] << endl;
}
}
// Finally, asking the user if they want to open another file.
choice = 'A';
while (choice != 'Y' && choice != 'N') {
cout << "\nWould you like to open another file? Y/N" << endl;
cin >> choice;
}
// Stops reading the file and preps for the next potential loop.
inFile.close();
cout << endl;
}
}
|