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
|
// CST 116 - Andrei Florea - Lab 3
// Take in a file input with data and display statistics about the file
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;
void getFileName(string&);
void readData(fstream&, int[], int[], int[], int[], float[], float[], float[], float[], float[]);
void displayData(int[], int[], int[], int[], float[], float[], float[], float[], float[], int);
int main()
{
// Create and initiate most variables, this is where the main function of code runs
int pick_up_loc[50];
int drop_off_loc[50];
int pass_count[50];
int trip[50];
float distance[50];
float fare_amount[50];
float toll_amount[50];
float total[50];
float cost_mi[50];
string file_name;
getFileName(file_name);
fstream data_file;
data_file.open(file_name);
if (data_file.is_open())
{
readData(data_file, pick_up_loc, drop_off_loc, pass_count, trip, distance, fare_amount, toll_amount, total, cost_mi);
}
else if (!(data_file.is_open()))
{
cout << "Trouble opening " << file_name << "." << endl;
}
}
void getFileName(string& name)
{
// Ask user for file name, store it in name
cout << "Enter the name of the file including extension: " << endl;
cin >> name;
}
void readData(fstream& file, int pick[], int drop[], int passenger[], int trip[], float distance[], float fare[], float toll[], float total[], float cost_mi[])
{
// Take in each line of data and store it in the correct slot for each array
int counter = 0;
file >> pick[counter] >> drop[counter] >> passenger[counter] >> distance[counter] >> fare[counter] >> toll[counter];
total[counter] = fare[counter] + toll[counter];
trip[counter] = counter + 1;
if (distance[counter] == 0)
{
cost_mi[counter] = 0;
}
else cost_mi[counter] = fare[counter] / distance[counter];
while( !file.eof() )
{
counter++;
file >> pick[counter] >> drop[counter] >> passenger[counter] >> distance[counter] >> fare[counter] >> toll[counter];
trip[counter] = counter + 1;
total[counter] = fare[counter] + toll[counter];
if (distance[counter] == 0)
{
cost_mi[counter] = 0;
}
else cost_mi[counter] = fare[counter] / distance[counter];
}
file.close();
displayData(trip, pick, drop, passenger, distance, fare, toll, total, cost_mi, counter);
}
void displayData(int trip[], int pick[], int drop[], int passenger[], float distance[], float fare[], float toll[], float total[], float cost_mi[], int counter)
{
// Using the arrays, display the data
int total_people = 0;
float total_cost = 0;
int width = 15;
cout << setw(width) << "Trip #" << setw(width) << "Pick Up" << setw(width) << "Drop off" << setw(width) << "# of ppl";
cout << setw(width) << "Distance" << setw(width) << "Fare" << setw(width) << "Toll";
cout << setw(width) << "Total" << setw(width) << "Cost/Mile" << setw(width) << endl;
for (int idx = 0; idx <= counter; idx++)
{
cout << setw(width) << trip[idx] << setw(width) << pick[idx] << setw(width) << drop[idx];
cout << setprecision(2) << fixed << setw(width) << passenger[idx] << setw(width) << distance[idx] << setw(width) << fare[idx];
cout << setw(width) << toll[idx] << setw(width) << total[idx] << setw(width) << cost_mi[idx] << endl;
total_people = total_people + passenger[idx];
total_cost = total_cost + fare[idx] + toll[idx];
}
cout << setw(width) << "People transported: " << total_people << endl;
cout << setw(width) << "Total paid: " << total_cost << endl;
cout << setw(width) << "Average cost per person: " << total_cost / total_people << endl;
}
|