From a76f16e10e073dace3fbefacb4a990ad46671b0c Mon Sep 17 00:00:00 2001 From: Musa Ahmed Date: Thu, 24 Nov 2022 11:25:53 -0800 Subject: added reading for each dataset in file --- BlankConsoleLab/BlankConsoleLab.cpp | 16 -- BlankConsoleLab/BlankConsoleLab.vcxproj | 10 +- BlankConsoleLab/BlankConsoleLab.vcxproj.filters | 2 +- BlankConsoleLab/Lab3-Ahmed.cpp | 204 ++++++++++++++++++++++++ BlankConsoleLab/out.txt | 17 ++ BlankConsoleLab/read.txt | 12 ++ BlankConsoleLab/small.txt | 11 ++ LabResults.txt | 1 - 8 files changed, 250 insertions(+), 23 deletions(-) delete mode 100644 BlankConsoleLab/BlankConsoleLab.cpp create mode 100644 BlankConsoleLab/Lab3-Ahmed.cpp create mode 100644 BlankConsoleLab/out.txt create mode 100644 BlankConsoleLab/read.txt create mode 100644 BlankConsoleLab/small.txt delete mode 100644 LabResults.txt diff --git a/BlankConsoleLab/BlankConsoleLab.cpp b/BlankConsoleLab/BlankConsoleLab.cpp deleted file mode 100644 index ed5f807..0000000 --- a/BlankConsoleLab/BlankConsoleLab.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// BlankConsoleLab.cpp : This file contains the 'main' function. Program execution begins and ends there. -// - -#include - -using namespace std; - -using std::cout; -using std::cin; -using std::endl; - -int main() -{ - cout << "Hello World!\n"; -} - diff --git a/BlankConsoleLab/BlankConsoleLab.vcxproj b/BlankConsoleLab/BlankConsoleLab.vcxproj index db2e734..e775e18 100644 --- a/BlankConsoleLab/BlankConsoleLab.vcxproj +++ b/BlankConsoleLab/BlankConsoleLab.vcxproj @@ -29,26 +29,26 @@ Application true - v142 + v143 Unicode Application false - v142 + v143 true Unicode Application true - v142 + v143 Unicode Application false - v142 + v143 true Unicode @@ -139,7 +139,7 @@ - + diff --git a/BlankConsoleLab/BlankConsoleLab.vcxproj.filters b/BlankConsoleLab/BlankConsoleLab.vcxproj.filters index aca1dd9..e357aea 100644 --- a/BlankConsoleLab/BlankConsoleLab.vcxproj.filters +++ b/BlankConsoleLab/BlankConsoleLab.vcxproj.filters @@ -15,7 +15,7 @@ - + Source Files diff --git a/BlankConsoleLab/Lab3-Ahmed.cpp b/BlankConsoleLab/Lab3-Ahmed.cpp new file mode 100644 index 0000000..9f41313 --- /dev/null +++ b/BlankConsoleLab/Lab3-Ahmed.cpp @@ -0,0 +1,204 @@ +/******************************************************************** +* File: Chapter 11 Debug.cpp +* +* General Instructions: Complete each step before proceeding to the +* next. +* +* Debugging Exercise 1 +* +* 1) Make your own data file like Troy 12, with the next person on the +* next line and save it to a directory you create on your drive. +* 2) Under the Project menu, select Add Existing Item and +* add the input file you just placed on your drive to your +* current project. Make sure your Solution Explorer window +* is visible. If not, you can display it by selecting Solution +* Explorer (or Ctrl+Alt+L). +* 3) Open the input file by simply double clicking the name of the +* file in your Solution Explorer. +* 4) Build and execute the program. +* 5) Update the file paths below to correctly represent the path you +* created. +* 6) Rebuild and execute the program. +* 7) Examine the code and the output and notice the use of +* parallel arrays. +* 8) Add the output file created via the execution of +* your program to your Project. Execute your program again +* and notice how Visual Studio has rewritten your output file +* and asks if you would like to reload the file (select Yes). +* 9) Examine the contents of both the input and the output file. +* 10) Fix all the problems in your code that exist in relation to +* the output. Verify that your output is appropriate for your + input file. +* 11) Build and execute your code until you have all errors +* removed and all the output is correct. +* +* Debugging Exercise 2 +* +* 1) Replace the double slashes (\\) in the input file open statement +* with only a single slash +* (i.e., inFile.open("C:\TEMP\Chap_11_data.txt"). +* 2) Build your code, noticing the impact of the invalid path you +* created in the previous step. +* 3) Replace the backslashes as they were. +* 4) Change both the input and output filenames so they are +* invalid. +* 5) Update the file related error messages within the code +* to also provide the specific name of the file that is having a +* problem. +* 6) Rebuild and execute your program to verify that your messages +* are correct. +* 7) Correct the path names. +* 8) Build and execute your code and carefully check your +* output on both the console and in the output file. +* +********************************************************************/ +#include +#include // For the files!!!! +#include // For manipulators & formatting options +#include +#include + +using std::cin; +using std::cout; +using std::endl; +using std::setw; +using std::ios; +using std::string; +using std::ifstream; +using std::ofstream; +using namespace std; + + +const int EMPLOYEES = 50; +const int MAX = 50; + +int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int passCount[], float distance[], float fare[], float toll[], float costPMile[], int counter, int people, int totalRecords, int paid, float totalFare[]); +void WriteOutputFile(ofstream& outFile, int totalRecords, int people, int pick[], int drop[], int passCount[], float distance[], float fare[], float toll[], float totalFare[], float costPMile[], int counter, float paid); +void PrintTotalsAndSummary(ofstream& outFile, int totalRecords, int people, int pick[], int drop[], int passCount[], float distance[], float fare[], float toll[], float totalFare[], float costPMile[], int counter, float paid); + +int main() +{ + int pick[EMPLOYEES]; + int drop[MAX]; + int passCount[EMPLOYEES]; + float distance[MAX]; + float fare[EMPLOYEES]; + float toll[MAX]; + float totalFare[MAX] = { 0 }; + float costPMile[MAX] = { 0 }; + int record_counter(0); + int people(0); + int counter = 0; + float paid = 0; + + string filename; + ifstream inFile; + + // Notice how this automatically opens the file + ofstream outFile("out.txt"); + cout << "Please enter the input file name including extension: " << endl; + cin >> filename; + inFile.open(filename); + + if (inFile.is_open()) + { + record_counter = ReadData(inFile, outFile, pick, drop, passCount, distance, fare, toll, costPMile, counter, people, record_counter, paid, totalFare); + inFile.close(); + + if (outFile.is_open()) + { + PrintTotalsAndSummary(outFile, record_counter, people, pick, drop, passCount, distance, fare, toll, totalFare, costPMile, counter, paid); + WriteOutputFile(outFile, record_counter, people, pick, drop, passCount, distance, fare, toll, totalFare, costPMile, counter, paid); + outFile.close(); + } + else + { + cout << "Trouble Opening File"; + cout << "\n\n\t\t ** About to EXIT NOW! ** "; + } + } + else + { + cout << "Trouble Opening File"; + cout << "\n\n\t\t ** About to EXIT NOW! ** "; + } + return 0; +} +int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int passCount[], float distance[], float fare[], float toll[], float costPMile[], int counter, int people, int totalRecords, int paid, float totalFare[]) +{ + + + + inFile >> pick[counter] >> drop[counter] >> passCount[counter] >> distance[counter] >> fare[counter] >> toll[counter]; // Priming Read + while (!inFile.eof()) + { + cout << setiosflags(ios::left) << setw(15) + << pick[counter] + << setw(15) << drop[counter] << setw(15) + << passCount[counter] + << setw(15) << distance[counter] << setw(15) + << fare[counter] + << setw(15) << toll[counter]; + if (distance[counter] == 0) { + costPMile[counter] = 0; + } + else { + costPMile[counter] = (toll[counter] + fare[counter]) / distance[counter]; + } + cout << setw(15) << costPMile[counter] << endl; + counter++; + inFile >> pick[counter] >> drop[counter] >> passCount[counter] >> distance[counter] >> fare[counter] >> toll[counter]; + + } + + return counter; +} +void WriteOutputFile(ofstream& outFile, int totalRecords, int people, int pick[], int drop[], int passCount[], float distance[], float fare[], float toll[], float totalFare[], float costPMile[], int counter, float paid) +{ + + + outFile << " Here is the Output File" << endl; + for (int r = 0; r < totalRecords; r++) + { + + outFile << setiosflags(ios::left) << setw(15) + << pick[r] + << setw(15) << drop[r] << setw(15) + << passCount[r] + << setw(15) << distance[r] << setw(15) + << fare[r] + << setw(15) << toll[r] + << setw(15) << costPMile[r] << endl; + } + +} +void PrintTotalsAndSummary(ofstream& outFile, int totalRecords, int people, int pick[], int drop[], int passCount[], float distance[], float fare[], float toll[], float totalFare[], float costPMile[], int count, float paid) +{ + for (int i = 0; i < totalRecords; i++) { + people += passCount[i]; + float temp = fare[i] + toll[i]; + paid += temp; + totalFare[i] = temp; + + if (distance == 0) { + costPMile[i] = 0; + } + else { + costPMile[i] = totalFare[i] / distance[i]; + // cout << costPMile[i]; + } + } + // To screen + cout << "\n\n\t** Total Records: " << totalRecords << " **\n" + << "\n\n\t** People Transported: " << people << " **\n" + << "\n\n\t** Total Cost: " << paid << " **\n" + + << "\t\t The End \n"; + + // To file + outFile << "\n\n\t** Total Records: " << totalRecords << " **\n" + << "\n\n\t** People Transported: " << people << " **\n" + << "\n\n\t** Total Cost: " << paid << " **\n" + << "\t\t The End \n"; +} + diff --git a/BlankConsoleLab/out.txt b/BlankConsoleLab/out.txt new file mode 100644 index 0000000..35ec2bd --- /dev/null +++ b/BlankConsoleLab/out.txt @@ -0,0 +1,17 @@ + + + ** Total Records: 6 ** + + + ** People Transported: 14 ** + + + ** Total Cost: 113.52 ** + The End + Here is the Output File +129 7 3 1.3 7.5 0 5.76923 +36 69 1 11.41 32 5.76 3.30938 +7 41 1 4.6 15 5.76 4.51304 +150 61 2 6.75 23 0 3.40741 +112 17 1 3.84 15 0 3.90625 +80 112 6 1.64 9.5 0 5.79268 diff --git a/BlankConsoleLab/read.txt b/BlankConsoleLab/read.txt new file mode 100644 index 0000000..2bfcbd5 --- /dev/null +++ b/BlankConsoleLab/read.txt @@ -0,0 +1,12 @@ +John 19 +Molly 23 +Tim 48 +Keil 42 +Trinh 21 +Musa 18 +Anthony 25 +Kevin 27 +Cheryl 32 +Kim 16 +Dave 25 +Will 34 \ No newline at end of file diff --git a/BlankConsoleLab/small.txt b/BlankConsoleLab/small.txt new file mode 100644 index 0000000..1fba087 --- /dev/null +++ b/BlankConsoleLab/small.txt @@ -0,0 +1,11 @@ +129 7 3 1.3 7.5 0 + +36 69 1 11.41 32 5.76 + +7 41 1 4.6 15 5.76 + +150 61 2 6.75 23 0 + +112 17 1 3.84 15 0 + +80 112 6 1.64 9.5 0 \ No newline at end of file diff --git a/LabResults.txt b/LabResults.txt deleted file mode 100644 index bfe34d2..0000000 --- a/LabResults.txt +++ /dev/null @@ -1 +0,0 @@ -RunResults \ No newline at end of file -- cgit v1.2.3