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
|
// BlankConsoleLab.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int MAX_INPUT_AMOUNT = 100;
void ReadData(ifstream& input, int startPos[MAX_INPUT_AMOUNT], int endPos[MAX_INPUT_AMOUNT],
int passengerCount[MAX_INPUT_AMOUNT], float distance[MAX_INPUT_AMOUNT], float fareAmount[MAX_INPUT_AMOUNT], float tollAmount[MAX_INPUT_AMOUNT], int& counter);
void ProcessData(int startPos[MAX_INPUT_AMOUNT], int endPos[MAX_INPUT_AMOUNT],
int passengerCount[MAX_INPUT_AMOUNT], float fareAmount[MAX_INPUT_AMOUNT], float tollAmount[MAX_INPUT_AMOUNT],
float distance[MAX_INPUT_AMOUNT], float totalCost[MAX_INPUT_AMOUNT], float costPerMile[MAX_INPUT_AMOUNT],
float& GrandTotal, int& totalPassengers,
int& counter);
void OutputData(int passengerCount[MAX_INPUT_AMOUNT], float fareAmount[MAX_INPUT_AMOUNT], float tollAmount[MAX_INPUT_AMOUNT],
float distance[MAX_INPUT_AMOUNT], float totalCost[MAX_INPUT_AMOUNT], float costPerMile[MAX_INPUT_AMOUNT],
float& GrandTotal, int& totalPassengers,
int& counter);
string getPadding(int input);
//C:\Users\wythe\Desktop\Homework\C++\cst116-lab3-johnson\small.txt
int main()
{
ifstream inputStream;
while (!inputStream.is_open())
{
string inputFilePath = "";
cout << "Please input the location of the data file.\n";
cin >> inputFilePath;
cout << endl;
if (inputFilePath[0] != 'C' || inputFilePath[1] != ':')
{
inputFilePath = "C:\\" + inputFilePath;
}
inputStream.open(inputFilePath);
if (!inputStream.is_open())
{
cout << "Invalid file path." << endl;
}
}
int startPos[MAX_INPUT_AMOUNT] = {};
int endPos[MAX_INPUT_AMOUNT] = {};
int passengerCount[MAX_INPUT_AMOUNT] = {};
float distance[MAX_INPUT_AMOUNT] = {};
float fareAmount[MAX_INPUT_AMOUNT] = {};
float tollAmount[MAX_INPUT_AMOUNT] = {};
int counter = 0;
ReadData(inputStream, startPos, endPos, passengerCount, distance, fareAmount, tollAmount, counter);
float totalCost[MAX_INPUT_AMOUNT] = {};
float costPerMile[MAX_INPUT_AMOUNT] = {};
float GrandTotal = 0;
int totalPassengers = 0;
ProcessData(startPos, endPos, passengerCount, fareAmount, tollAmount, distance, totalCost, costPerMile, GrandTotal, totalPassengers, counter);
OutputData(passengerCount, fareAmount, tollAmount, distance, totalCost, costPerMile, GrandTotal, totalPassengers, counter);
}
void ReadData(ifstream& input, int startPos[MAX_INPUT_AMOUNT], int endPos[MAX_INPUT_AMOUNT],
int passengerCount[MAX_INPUT_AMOUNT], float distance[MAX_INPUT_AMOUNT], float fareAmount[MAX_INPUT_AMOUNT], float tollAmount[MAX_INPUT_AMOUNT], int& counter)
{
while (!input.eof())
{
input >> startPos[counter];
input >> endPos[counter];
int passengers = 0;
input >> passengers;
if (passengers > 9) passengers = 9;
passengerCount[counter] = passengers;
input >> distance[counter];
input >> fareAmount[counter];
input >> tollAmount[counter];
counter++;
}
}
void ProcessData(int startPos[MAX_INPUT_AMOUNT], int endPos[MAX_INPUT_AMOUNT],
int passengerCount[MAX_INPUT_AMOUNT], float fareAmount[MAX_INPUT_AMOUNT], float tollAmount[MAX_INPUT_AMOUNT],
float distance[MAX_INPUT_AMOUNT], float totalCost[MAX_INPUT_AMOUNT], float costPerMile[MAX_INPUT_AMOUNT],
float& GrandTotal, int& totalPassengers,
int& counter)
{
for (int i = 0; i <= counter; i++)
{
totalCost[i] = fareAmount[i] + tollAmount[i];
costPerMile[i] = distance[i] == 0 ? 0 : fareAmount[i] / distance[i];
GrandTotal += totalCost[i];
totalPassengers += passengerCount[i];
}
}
void OutputData(int passengerCount[MAX_INPUT_AMOUNT], float fareAmount[MAX_INPUT_AMOUNT], float tollAmount[MAX_INPUT_AMOUNT],
float distance[MAX_INPUT_AMOUNT], float totalCost[MAX_INPUT_AMOUNT], float costPerMile[MAX_INPUT_AMOUNT],
float& GrandTotal, int& totalPassengers,
int& counter)
{
cout << setprecision(3);
cout << "|Ride # |Passengers|Distance |Fare Cost| Toll $ |Total $ |$Per Mile|\n";
for (int i = 0; i <= counter; i++)
{
cout << "|" << i + 1 << getPadding(i + 1) << passengerCount[i] << getPadding(passengerCount[i]) << distance[i] << getPadding(distance[i]) << fareAmount[i] << getPadding(fareAmount[i]) <<
tollAmount[i] << getPadding(tollAmount[i]) << totalCost[i] << getPadding(totalCost[i]) << costPerMile[i] << getPadding(costPerMile[i]) << endl;
}
cout << "\n\n Grand Total: " << GrandTotal << "$\n Total Passengers: " << totalPassengers << endl;
}
string getPadding(int input)
{
string padding = " |";
if (input >= 10) padding = " |";
if (input >= 100) padding = " |";
if (input >= 1000) padding = " |";
return padding;
}
|