diff options
| author | Taylor Rogers <[email protected]> | 2022-11-27 12:34:36 -0800 |
|---|---|---|
| committer | Taylor Rogers <[email protected]> | 2022-11-27 12:34:36 -0800 |
| commit | 2d3ab4f111f1f760717ef067b5f68399093ec4b3 (patch) | |
| tree | 5adba244f2bf7af8cb3142d9c11239c14fcb0cb6 /BlankConsoleLab/BlankConsoleLab.cpp | |
| parent | Added directives (diff) | |
| download | cst116-lab3-taylorrog-2d3ab4f111f1f760717ef067b5f68399093ec4b3.tar.xz cst116-lab3-taylorrog-2d3ab4f111f1f760717ef067b5f68399093ec4b3.zip | |
Added filename function
Diffstat (limited to 'BlankConsoleLab/BlankConsoleLab.cpp')
| -rw-r--r-- | BlankConsoleLab/BlankConsoleLab.cpp | 102 |
1 files changed, 101 insertions, 1 deletions
diff --git a/BlankConsoleLab/BlankConsoleLab.cpp b/BlankConsoleLab/BlankConsoleLab.cpp index 70f43fd..0716815 100644 --- a/BlankConsoleLab/BlankConsoleLab.cpp +++ b/BlankConsoleLab/BlankConsoleLab.cpp @@ -13,10 +13,110 @@ using std::setw; using std::ios; using std::ifstream; using std::ofstream; +using std::string; +const int MAX = 100; + +int ReadData(ifstream& inFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[]); +void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], + int counter); +void PrintTotalsAndSummary(ofstream& out, int totalRecords); int main() { - cout << "Hello World!\n"; + + int pick[MAX]; + int drop[MAX]; + int psgr[MAX]; + float dist[MAX]; + float fare[MAX]; + float toll[MAX]; + int record_counter(0); + + string filename; + + cout << "Enter the file name you wish to read data from: "; + cin >> filename; + cout << endl; + + ifstream inFile; + + // Notice how this automatically opens the file + ofstream outFile("lab3_Report.txt"); + + inFile.open(filename); + + if (inFile.is_open()) + { + record_counter = ReadData(inFile, pick, drop, psgr, dist, fare, toll); + inFile.close(); + + if (outFile.is_open()) + { + WriteOutputFile(outFile, pick, drop, psgr, dist, fare, toll, record_counter); + PrintTotalsAndSummary(outFile, record_counter); + 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, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[]) +{ + int counter = 0; + inFile >> pick[counter] >> drop[counter] >> psgr[counter] >> dist[counter] >> fare[counter] >> toll[counter]; // Priming Read + while (!inFile.eof()) + { + 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; + counter++; + inFile >> pick[counter] >> drop[counter] >> psgr[counter] >> dist[counter] >> fare[counter] >> toll[counter]; + } + + return counter; +} + +void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], + int counter) +{ + outFile << " Here is the Output File" << endl; + for (int r = 0; r <= counter - 1; r++) + { + 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; + } +} + +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"; +} |