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
|
/*This is Lab 3 assist program. It does not do Lab 3 specs BUT it will
help ALOT!!!
Not many comments shown in order to require students to think
Use an input file with 6 columns of numbers as required in Lab 3 Write up.
Place file in correct path.
*/
#include <iostream> //For printing to the screen
#include <fstream> //For the files
#include <iomanip> //For manipulators & formatting options
using std::cin; //Allows use of cin
using std::cout; //Allows use of cout
using std::endl; //Allows end line
using std::setw; //Allows set width
using std::ios; //Allows input/output stream commands
using std::ifstream; //Allows input file stream
using std::ofstream; //Allows output file stream
const int MAX = 50; //Adds a constant int of 50, for the maximum number of records
int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int psgr[], float
dist[], float fare[], float toll[]); //Defines function that reads data from the input file
void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int
psgr[], float dist[], float fare[], float toll[],
int counter); //Defines function that prints the data from input to output file
void PrintTotalsAndSummary(ofstream& out, int totalRecords); //Defines function that prints total records to screen and output file
int main() //Function main
{
int pick[MAX]; //Defines int array for pick up
int drop[MAX]; //Defines int array for drop off
int psgr[MAX]; //Defines int array for passengers
float dist[MAX]; //Defines float array for distance
float fare[MAX]; //Defines float array for fare
float toll[MAX]; //Defines float array for toll
int record_counter(0); //Defines an int that tracks the number of records taken
ifstream inFile; //Adds an infile to be used later
//Notice how this automatically opens the file
ofstream outFile; //Adds an outfile to be used later
outFile.open("C:\\TEMP1\\lab3_Report.txt");
char file = 'a'; //sets variable to change input file
cout << "Please choose a file to open. Press 's' for small.txt"
<< ", or press 'l' for large.txt: "; //Prompts user for input file choice
cin >> file; //Reads in users choice for input file
cout << endl; //Prints new line
switch (file) { //switch statement for variable file
case 's': //case when user inputs s for small
inFile.open("C:\\TEMP1\\small.txt"); //open small.txt
break; //end case
case 'l': //case when user inputs l for large
inFile.open("C:\\TEMP1\\large.txt"); //open large.txt
break; //end case
}
if (inFile.is_open()) //If the infile is open
{
record_counter = ReadData(inFile, outFile, pick, drop, psgr, dist,
fare, toll); //Do the ReadData function and set it as new variable record_counter
inFile.close(); //Close the input file
if (outFile.is_open()) //If the output file is open
{
WriteOutputFile(outFile, pick, drop, psgr,
dist, fare, toll, record_counter); //Do the WriteOutptFile function
PrintTotalsAndSummary(outFile, record_counter); //Do the PrintTotalsAndSummary function
outFile.close(); //Close the output file
}
else //Otherwise, do this
{
cout << "Trouble Opening Output File"; //Print text
cout << "\n\n\t\t ** About to EXIT NOW! ** "; //Print text
}
}
else //Otherwise, do this
{
cout << "Trouble Opening Input File"; //Print text
cout << "\n\n\t\t ** About to EXIT NOW! ** "; //Print text
}
return 0; //Return empty value and end stream
}
int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int psgr[], float
dist[], float fare[], float toll[]) //Function to ReadData from the input file
{
int counter = 0; //Set variable counter
inFile >> pick[counter] >> drop[counter] >> psgr[counter] >>
dist[counter] >> fare[counter] >> toll[counter]; //Priming Read
while (!inFile.eof()) //While not the end of the in file
{
cout << setiosflags(ios::left) << setw(5)
<< pick[counter] << resetiosflags(ios::left)
<< setw(10) << drop[counter] <<
resetiosflags(ios::left)
<< setw(12) << psgr[counter] <<
resetiosflags(ios::left)
<< setw(14) <<
dist[counter] << resetiosflags(ios::left)
<< setw(14) << fare[counter] <<
resetiosflags(ios::left)
<< setw(14) << toll[counter]
<< endl; //Print infile data as a table
float total_fare = fare[counter] + toll[counter]; //Adds float total_fare
cout << "Total fare: " << total_fare << "\n\n"; //Prints total_fare to the screen
counter++; //Add 1 to counter
inFile >> pick[counter] >> drop[counter] >> psgr[counter]
>> dist[counter] >> fare[counter] >> toll[counter]; //Sets all of the arrays values to the new counter value
}
return counter; //Return variable counter
}
void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int
psgr[], float dist[], float fare[], float toll[], int counter) //Function to write the output file
{
outFile << " Here is the Output File" << endl; //Print text to the out file
for (int r = 0; r <= counter - 1; r++) //Loop while there is still data in the input file
{
outFile << setiosflags(ios::left) << setw(5)
<< pick[r] << resetiosflags(ios::left)
<< setw(10) << drop[r] <<
resetiosflags(ios::left)
<< setw(12) << psgr[r] <<
resetiosflags(ios::left)
<< setw(14) <<
dist[r] << resetiosflags(ios::left)
<< setw(14) << fare[r] <<
resetiosflags(ios::left)
<< setw(14) << toll[r]
<< endl; //Print in file data as a table in the out file
float total_fare = fare[r] + toll[r]; //Adds variable total_fare
outFile << "Total fare: " << total_fare << "\n\n"; //Prints total_fare to the out file
}
}
void PrintTotalsAndSummary(ofstream& outFile, int totalRecords) //Function to print the total records
{
// To screen
cout << "\n\n\t** Total Records: " << totalRecords << " **\n" //Prints the total records to the screen
<< "\t\t The End \n"; //Prints The End to the screen
// To file
outFile << "\n\n\t** Total Records: " << totalRecords << " **\n" //Prints the total records to the out file
<< "\t\t The End \n"; //Prints The End to the out file
}
|