diff options
Diffstat (limited to 'BlankConsoleLab/cst116-lab3-wilson.cpp')
| -rw-r--r-- | BlankConsoleLab/cst116-lab3-wilson.cpp | 170 |
1 files changed, 120 insertions, 50 deletions
diff --git a/BlankConsoleLab/cst116-lab3-wilson.cpp b/BlankConsoleLab/cst116-lab3-wilson.cpp index 66fc614..6d151da 100644 --- a/BlankConsoleLab/cst116-lab3-wilson.cpp +++ b/BlankConsoleLab/cst116-lab3-wilson.cpp @@ -1,17 +1,21 @@ -/*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. -*/ + +/*Jacob Wilson +cst116 +Lab 3 +11/28/2022*/ + +///////////////////////////// Pre Compiler Directives ///////////////////////////// #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 @@ -19,14 +23,21 @@ using std::ofstream; //Allows output file stream const int MAX = 50; //Adds a constant int of 50, for the maximum number of records +///////////////////////////// Function Definitions ///////////////////////////// + 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 +void PrintTotalsAndSummary(ofstream& out, int totalRecords, int psgr[], int num_psgr, + float fare[], float toll[], float total_fare); //Defines function that prints total records to screen and output file +int NumPsgr(int psgr[], int num_psgr, int totalRecords); //Defines function that calculates the number of passengers total +int TotalPaid(float fare[], float toll[], float total_fare, int totalRecords); -int main() //Function main +///////////////////////////// Main Function ///////////////////////////// + +int main() { int pick[MAX]; //Defines int array for pick up int drop[MAX]; //Defines int array for drop off @@ -34,18 +45,19 @@ int main() //Function main 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 + int num_psgr = 0; //Defines variable for number of passengers + float total_fare = 0.0; 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"); + + outFile.open("C:\\TEMP1\\lab3_Report.txt"); //Opens the designated output file 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 + 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 @@ -53,6 +65,7 @@ int main() //Function main 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 @@ -67,7 +80,7 @@ int main() //Function main { WriteOutputFile(outFile, pick, drop, psgr, dist, fare, toll, record_counter); //Do the WriteOutptFile function - PrintTotalsAndSummary(outFile, record_counter); //Do the PrintTotalsAndSummary function + PrintTotalsAndSummary(outFile, record_counter, psgr, num_psgr, fare, toll, total_fare); //Do the PrintTotalsAndSummary function outFile.close(); //Close the output file } else //Otherwise, do this @@ -83,29 +96,42 @@ int main() //Function main } return 0; //Return empty value and end stream } + +///////////////////////////// Read Data from Input File Function ///////////////////////////// + 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 + dist[], float fare[], float toll[]) { int counter = 0; //Set variable counter inFile >> pick[counter] >> drop[counter] >> psgr[counter] >> dist[counter] >> fare[counter] >> toll[counter]; //Priming Read + + cout << setiosflags(ios::left) << setw(5) << "Pick Up" + << resetiosflags(ios::left) << setw(10) << "Drop Off" + << resetiosflags(ios::left) << setw(12) << "Passengers" + << resetiosflags(ios::left) << setw(14) << "Distance" + << resetiosflags(ios::left) << setw(14) << "Fare" + << resetiosflags(ios::left) << setw(14) << "Toll" << "\n\n"; //Print headers to the screen + 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 + 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]; //Defines float total_fare + cout << "Total fare: " << total_fare << "\n"; //Prints total_fare to the screen + + float cost_per_mile = fare[counter] / dist[counter]; //Defines float cost_per_mile + if (dist[counter] != 0) { //If distance is not 0 + cout << "Cost per mile: " << cost_per_mile << "\n\n"; //Print cost per mile + } + else { //Otherwise + cout << "Cost per mile: 0\n"; //Print the cost per mile as 0 + } counter++; //Add 1 to counter inFile >> pick[counter] >> drop[counter] >> psgr[counter] @@ -113,35 +139,79 @@ int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int ps } return counter; //Return variable counter } + +///////////////////////////// Write Output File Function ///////////////////////////// + void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int - psgr[], float dist[], float fare[], float toll[], int counter) //Function to write the output file + psgr[], float dist[], float fare[], float toll[], int counter) { outFile << " Here is the Output File" << endl; //Print text to the out file + + outFile << setiosflags(ios::left) << setw(5) << "Pick Up" + << resetiosflags(ios::left) << setw(10) << "Drop Off" + << resetiosflags(ios::left) << setw(12) << "Passenger" + << resetiosflags(ios::left) << setw(14) << "Distance" + << resetiosflags(ios::left) << setw(14) << "Fare" + << resetiosflags(ios::left) << setw(14) << "Toll" << "\n\n"; //Print headers 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 + 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 + outFile << "Total fare: " << total_fare << "\n"; //Prints total_fare to the out file + + float cost_per_mile = fare[r] / dist[r]; //Define float for cost per mile + if (dist[r] != 0) { //If the value of dist array is not 0 + outFile << "Cost per mile: " << cost_per_mile << "\n\n"; //Print cost per mile to out file + } + else { //Otherwise + outFile << "Cost per mile: 0\n\n"; //Print cost per mile as 0 (can't divide by 0) + } + } +} + +///////////////////////////// People Traansported Function ///////////////////////////// + +int NumPsgr(int psgr[], int num_psgr, int totalRecords) +{ + for (int i = 0; i < totalRecords; i++) //Do for the number of records + { + num_psgr += psgr[i]; //Take number of passengers and add the value in psgr array } + return num_psgr; //Return number of passengers } -void PrintTotalsAndSummary(ofstream& outFile, int totalRecords) //Function to print the total records + + + +int TotalPaid(float fare[], float toll[], float total_fare, int totalRecords) +{ + for (int j = 0; j < totalRecords; j++) //Do for the number of records + { + total_fare = total_fare + (fare[j] + toll[j]); //Take number of passengers and add the value in psgr array + } + return total_fare; //Return number of passengers +} + +///////////////////////////// Print Totals and Summary Function ///////////////////////////// + +void PrintTotalsAndSummary(ofstream& outFile, int totalRecords, int psgr[], int num_psgr, float fare[], float toll[], float total_fare) { // 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 + cout << "\n\t** People Transported: " << NumPsgr(psgr, num_psgr, totalRecords) << " **\n"; //Prints the total records to the screen + cout << "\n\t** Total Paid (rounded down to nearest dollar): " + << TotalPaid(fare, toll, total_fare, totalRecords) << " **\n"; //Prints the total paid (all fares and tolls) to the screen + + cout << "\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 + outFile << "\n\t** People Transported: " << NumPsgr(psgr, num_psgr, totalRecords) << " **\n"; //Prints the total records to the out file + outFile << "\n\t** Total Paid (rounded down to nearest dollar): " + << TotalPaid(fare, toll, total_fare, totalRecords) << " **\n"; //Prints the total paid (all fares and tolls) to the out file + + outFile << "\t\t The End \n"; //Prints The End to the out file }
\ No newline at end of file |