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
148
149
150
151
152
153
154
155
|
/********************************************************************
* 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.
* - 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.
* 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.
* - 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.
*
* 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").
* - 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
* 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 <iostream>
#include <fstream> // For the files!!!!
#include <iomanip> // For manipulators & formatting options
#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;
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()
{
// 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("../Chap_11_Report.txt");
inFile.open("../Chap_11_data.txt");
if (inFile.is_open())
{
ReadData(inFile, names, ages);
inFile.close();
if (outFile.is_open()) // I'm pretty sure this us unnecessary. Afaik ofstream just creates the file if it doesn't exist already.
{
WriteOutput(outFile, names, ages);
PrintTotalsAndSummary(outFile, size(names));
outFile.close();
}
else
{
cout << "Trouble Opening Output File";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
}
else
{
cout << "Trouble Opening Input File";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
return 0;
}
void ReadData(ifstream& inFile, vector<string>& names, vector<int>& ages)
{
string name;
int age;
while (inFile >> name >> age)
{
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().
}
}
void WriteOutput(ofstream& outFile, vector<string> names, vector<int> ages)
{
outFile << " Here is the Output File" << endl;
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.
{
// 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)
{
// 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";
}
|