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
|
// BlankConsoleLab.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::ios;
using std::left;
using std::right;
using std::ifstream;
using std::ofstream;
const int EMPLOYEES = 20;
const int MAX = 150;
int ReadData(ifstream& inFile, int pickup[], int dropoff[], int psgrs[], float dist[], float fare[], float toll[]);
void WriteOutputFile(ofstream& outFile, int pickup[], int dropoff[], int psgrs[], float dist[], float fare[], float toll[], int record_counter);
void PrintTotalsAndSummary(ofstream& out, int totalRecords);
int main()
{
int pickup[MAX];
int dropoff[MAX];
int psgrs[MAX];
float dist[MAX];
float fare[MAX];
float toll[MAX];
int record_counter(0);
ifstream inFile;
ofstream outFile("C:\\Users\\prest\\source\\repos\\cst116-lab3-prestonderek\\LabResults.txt");
inFile.open("C:\\Users\\prest\\source\\repos\\cst116-lab3-prestonderek\\large.txt");
if (inFile.is_open())
{
record_counter = ReadData(inFile, pickup, dropoff, psgrs, dist, fare, toll);
inFile.close();
if (outFile.is_open())
{
WriteOutputFile(outFile, pickup, dropoff, psgrs, dist, fare, toll, record_counter);
PrintTotalsAndSummary(outFile, record_counter);
outFile.close();
}
else //Only outputs this else if the input file path is good. Otherwise it just jumps to the other else.
{
cout << "Trouble Opening Output File";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
}
else
{
cout << "Trouble Opening Input File";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
return 0;
}
//Read data function grabs the data from the file and adds each data type to its own array
int ReadData(ifstream& inFile, int pickup[], int dropoff[], int psgrs[], float dist[], float fare[], float toll[])
{
int counter = 0;
inFile >> pickup[counter] >> dropoff[counter] >> psgrs[counter] >>
dist[counter] >> fare[counter] >> toll[counter]; // Priming Read
//This writes the headers before the data is printed in the console.
cout << setw(5) << "Pickup: ";
cout << setw(10) << " Dropoff:";
cout << setw(12) << "Psgrs:";
cout << setw(12) << "Distance:";
cout << setw(12) << "Fare:";
cout << setw(12) << "Tolls:";
cout << endl;
while (!inFile.eof())
{
if (toll[counter] == 0) //This was added for a cleaner reading output. It replaces the toll if 0 with 'N/A'
{
cout << setiosflags(ios::left) << setw(5)
<< pickup[counter] << resetiosflags(ios::left)
<< setw(10) << dropoff[counter] <<
resetiosflags(ios::left)
<< setw(12) << psgrs[counter] <<
resetiosflags(ios::left)
<< setw(14) <<
dist[counter] << resetiosflags(ios::left)
<< setw(14) << fare[counter] <<
resetiosflags(ios::left)
<< setw(14) << "N/A"
<< endl;
}
else
cout << setiosflags(ios::left) << setw(5)
<< pickup[counter] << resetiosflags(ios::left)
<< setw(10) << dropoff[counter] <<
resetiosflags(ios::left)
<< setw(12) << psgrs[counter] <<
resetiosflags(ios::left)
<< setw(14) <<
dist[counter] << resetiosflags(ios::left)
<< setw(14) << fare[counter] <<
resetiosflags(ios::left)
<< setw(14) << toll[counter]
<< endl;
counter++;
inFile >> pickup[counter] >> dropoff[counter] >> psgrs[counter]
>> dist[counter] >> fare[counter] >> toll[counter];
}
return counter;
}
//The write output file function writes the information grabbed from the read data
//function to a new output file at the originally called location
void WriteOutputFile(ofstream& outFile, int pickup[], int dropoff[], int
psgrs[], float dist[], float fare[], float toll[],
int counter)
{
//This writes the headers in the output file before the data is written.
outFile << " Here is the Output File" << endl;
outFile << setw(5) << "Pickup: ";
outFile << setw(10) << " Dropoff:";
outFile << setw(12) << "Psgrs:";
outFile << setw(12) << "Distance:";
outFile << setw(12) << "Fare:";
outFile << setw(12) << "Tolls:";
outFile << endl;
for (int r = 0; r <= counter - 1; r++)
{
if (toll[r] == 0) //again, this is used for a cleaner looking output file.
{
outFile << setiosflags(ios::left) << setw(5)
<< pickup[r] << resetiosflags(ios::left)
<< setw(10) << dropoff[r] <<
resetiosflags(ios::left)
<< setw(12) << psgrs[r] <<
resetiosflags(ios::left)
<< setw(14) <<
dist[r] << resetiosflags(ios::left)
<< setw(14) << fare[r] <<
resetiosflags(ios::left)
<< setw(14) << "N/A"
<< endl;
}
else
outFile << setiosflags(ios::left) << setw(5)
<< pickup[r] << resetiosflags(ios::left)
<< setw(10) << dropoff[r] <<
resetiosflags(ios::left)
<< setw(12) << psgrs [r] <<
resetiosflags(ios::left)
<< setw(14) <<
dist[r] << resetiosflags(ios::left)
<< setw(14) << fare[r] <<
resetiosflags(ios::left)
<< setw(14) << toll[r]
<< endl;
}
}
//The print totals and summary lets you know how many records are added at the end of the console or output file.
void PrintTotalsAndSummary(ofstream& outFile, int totalRecords)
{
// To screen
cout << "\n\n\t** Total Records: " << totalRecords << " **\n"
<< "\t\t The End \n";
// To file
outFile << "\n\n\t** Total Records: " << totalRecords << " **\n"
<< "\t\t The End \n";
}
|