diff options
| -rw-r--r-- | CST116-Ch11-Debugging/CST116-Ch11-Debugging.cpp | 87 | ||||
| -rw-r--r-- | Chap_11_Report.txt | 11 | ||||
| -rw-r--r-- | Chap_11_data.txt | 1 |
3 files changed, 59 insertions, 40 deletions
diff --git a/CST116-Ch11-Debugging/CST116-Ch11-Debugging.cpp b/CST116-Ch11-Debugging/CST116-Ch11-Debugging.cpp index 53830da..cfbbc3c 100644 --- a/CST116-Ch11-Debugging/CST116-Ch11-Debugging.cpp +++ b/CST116-Ch11-Debugging/CST116-Ch11-Debugging.cpp @@ -8,11 +8,17 @@ * * 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. +* - Or use relative paths like a sane person. +* * 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). +* - I never do this. It's just as easy to open the file via File Explorer +* and adding it to the project just clutters things up. Also, the +* Solution Explorer has a folder view. +* * 3) Open the input file by simply double clicking the name of the * file in your Solution Explorer. * 4) Build and execute the program. @@ -28,7 +34,10 @@ * 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. +* input file. +* - I wish I could say I fixed it, but I'm pretty sure this +* is more like "I rewrote all of it from scratch". +* * 11) Build and execute your code until you have all errors * removed and all the output is correct. * @@ -37,9 +46,13 @@ * 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"). +* - Relative paths, people. Relative paths. +* * 2) Build your code, noticing the impact of the invalid path you * created in the previous step. * 3) Replace the backslashes as they were. +* - You still don't need the backslashes. Use single forward slashes instead. +* * 4) Change both the input and output filenames so they are * invalid. * 5) Update the file related error messages within the code @@ -55,85 +68,79 @@ #include <iostream> #include <fstream> // For the files!!!! #include <iomanip> // For manipulators & formatting options -using std::cin; -using std::cout; -using std::endl; -using std::setw; -using std::ios; - -using std::ifstream; -using std::ofstream; +#include <vector> // Vectors are better than arrays. This is an objective fact. +using namespace std; // Don't define each function individually...just don't const int EMPLOYEES = 20; const int MAX = 21; -int ReadData(ifstream& inFile, ofstream& outFile, char name[][MAX], int age[]); -void WriteOutputFile(ofstream& outFile, char name[][MAX], int age[], - int counter); +void ReadData(ifstream& inFile, vector<string>& names, vector<int>& ages); +void WriteOutput(ofstream& outFile, vector<string> names, vector<int> ages); void PrintTotalsAndSummary(ofstream& out, int totalRecords); int main() { - char name[EMPLOYEES][MAX]; - int age[EMPLOYEES]; - int record_counter(0); + // Names and ages are now stored in vectors, and I've removed the counter variable + // Because it was unnecesary even in the original program. + vector<string> names = {}; + vector<int> ages = {}; ifstream inFile; // Notice how this automatically opens the file - ofstream outFile("C:\\TEMP\\Chap_11_Report.txt"); + ofstream outFile("../Chap_11_Report.txt"); - inFile.open("C:\\TEMP\\Chap_11_data.txt"); + inFile.open("../Chap_11_data.txt"); if (inFile.is_open()) { - record_counter = ReadData(inFile, outFile, name, age); + ReadData(inFile, names, ages); inFile.close(); - if (outFile.is_open()) + if (outFile.is_open()) // I'm pretty sure this us unnecessary. Afaik ofstream just creates the file if it doesn't exist already. { - WriteOutputFile(outFile, name, age, record_counter); - PrintTotalsAndSummary(outFile, record_counter); + + WriteOutput(outFile, names, ages); + PrintTotalsAndSummary(outFile, size(names)); outFile.close(); } else { - cout << "Trouble Opening File"; + cout << "Trouble Opening Output File"; cout << "\n\n\t\t ** About to EXIT NOW! ** "; } } else { - cout << "Trouble Opening File"; + cout << "Trouble Opening Input File"; cout << "\n\n\t\t ** About to EXIT NOW! ** "; } return 0; } -int ReadData(ifstream& inFile, ofstream& outFile, char name[][MAX], int age[]) +void ReadData(ifstream& inFile, vector<string>& names, vector<int>& ages) { - int counter = 0; - inFile >> name[counter] >> age[counter]; // Priming Read - - while (!inFile.eof()) + string name; + int age; + while (inFile >> name >> age) { - cout << setiosflags(ios::left) << setw(25) - << name[counter] << resetiosflags(ios::left) - << setw(4) << age[counter] << endl; - counter++; - inFile >> name[counter] >> age[counter]; + name = ""; age = 0; + inFile >> name >> age; + names.push_back(name); // This is why vectors are better. + ages.push_back(age); // Look how much shorter this is when you have push_back(). } - - return counter; } -void WriteOutputFile(ofstream& outFile, char name[][MAX], int age[], int counter) +void WriteOutput(ofstream& outFile, vector<string> names, vector<int> ages) { outFile << " Here is the Output File" << endl; - for (int r = 0; r <= counter; r++) + for (int i = 0; i < size(names); i++) // Told you the counter was unecessary. Also, god help you if there are more ages than names. { - outFile << setiosflags(ios::left) << setw(25) - << name[r] << setw(4) - << resetiosflags(ios::left) << age[r] + // You do realize you can replace "setiosflags(ios::left)" with just "left", right? + outFile << left << setw(25) << names[i] + << setw(7) << right << ages[i] << endl; + cout << left << setw(25) << names[i] + << setw(7) << right << ages[i] + << endl; } } void PrintTotalsAndSummary(ofstream& outFile, int totalRecords) diff --git a/Chap_11_Report.txt b/Chap_11_Report.txt new file mode 100644 index 0000000..71af85c --- /dev/null +++ b/Chap_11_Report.txt @@ -0,0 +1,11 @@ + Here is the Output File +Molly 23 +Keil 42 +Anthony 25 +Cheryl 32 +Dave 25 +xX_YeetMaster_Xx 69420 + + + ** Total Records: 6 ** + The End diff --git a/Chap_11_data.txt b/Chap_11_data.txt index e4083f4..2aabf10 100644 --- a/Chap_11_data.txt +++ b/Chap_11_data.txt @@ -9,3 +9,4 @@ Cheryl 32 Kim 16
Dave 25
Will 34
+xX_YeetMaster_Xx 69420
\ No newline at end of file |