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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
//Code by Jordan Harris-Toovy for OIT's CST116 lab 8, November 2021
//13b - 11.9 Learn by Doing Exercises # 1:
/*
#include <iostream>
using namespace std;
#define MAX_VAL 15
void bubSort(float[MAX_VAL], int);
float findMedian(float[MAX_VAL], int);
int main()
{
float testArray[MAX_VAL] = { 6.9, 3.34, 1.99, 2.16, 6.88, 0.64, 7.93, -9.24, -9.88, 7.08, -0.9, 4.78, -7.91, -0.56, 5.17 };
cout << "From the array: ";
for (int idx = 0; idx < MAX_VAL; idx++)
{
cout << testArray[idx] << " ";
}
cout << "\nThe median is: ";
cout << findMedian(testArray, MAX_VAL) << endl;
return (0);
}
void bubSort(float arr[MAX_VAL], int len)
{
int pass = 0;
float hold = 0.0F;
while (pass <= len)
{
for (int idx = 0; idx < (len - 1); idx++)
{
if (arr[idx] > arr[idx + 1])
{
hold = arr[idx + 1];
arr[idx + 1] = arr[idx];
arr[idx] = hold;
pass = 0;
}
else
{
pass++;
}
}
}
}
float findMedian(float arr[MAX_VAL], int len)
{
bubSort(arr, len);
int center = int(floor((len / 2)));
if ((len % 2) != 0)
{
return arr[center];
}
else
{
return ((arr[center] + arr[center - 1]) / 2);
}
}
*/
//13c - 11.13 Debugging Exercise:
/********************************************************************
* 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 <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;
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 PrintTotalsAndSummary(ofstream& out, int totalRecords);
int main()
{
char name[EMPLOYEES][MAX];
int age[EMPLOYEES];
int record_counter(0);
ifstream inFile;
// Notice how this automatically opens the file
ofstream outFile("C:\\Users\\jorda\\source\\repos\\cst115-lab8-JordanHT-OIT\\CST116F2021-Lab8\\Report.txt");
inFile.open("C:\\Users\\jorda\\source\\repos\\cst115-lab8-JordanHT-OIT\\CST116F2021-Lab8\\Data.txt");
if (inFile.is_open())
{
record_counter = ReadData(inFile, outFile, name, age);
inFile.close();
if (outFile.is_open())
{
WriteOutputFile(outFile, name, age, record_counter);
PrintTotalsAndSummary(outFile, record_counter);
outFile.close();
}
else
{
cout << "Cannot open Report.txt";
cout << "\n\n\t\t ** Exiting ** ";
}
}
else
{
cout << "Cannot open Data.txt";
cout << "\n\n\t\t ** Exiting ** ";
}
return 0;
}
int ReadData(ifstream& inFile, ofstream& outFile, char name[][MAX], int age[])
{
int counter = 0;
inFile >> name[counter] >> age[counter]; // Priming Read
while (!inFile.eof())
{
cout << setiosflags(ios::left) << setw(25)
<< name[counter] << resetiosflags(ios::left)
<< setw(4) << age[counter] << endl;
counter++;
inFile >> name[counter] >> age[counter];
}
return counter;
}
void WriteOutputFile(ofstream& outFile, char name[][MAX], int age[], int counter)
{
outFile << " Here is the Output File" << endl;
for (int r = 0; r < counter; r++)
{
outFile << setiosflags(ios::left) << setw(25)
<< name[r] << setw(4)
<< resetiosflags(ios::left) << age[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";
}
*/
//13c - 11.14 Programming Exercises #1:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#define MAX_LENGTH 20
#define MAX_ENTRIES 15
int getInput(ifstream&, char[MAX_ENTRIES][MAX_LENGTH], char[MAX_ENTRIES][MAX_LENGTH], char[MAX_ENTRIES][12], float[MAX_ENTRIES], int[MAX_ENTRIES], char[MAX_ENTRIES]);
void displayInfo(char[MAX_ENTRIES][MAX_LENGTH], char[MAX_ENTRIES][MAX_LENGTH], char[MAX_ENTRIES][12], float[MAX_ENTRIES], int[MAX_ENTRIES], float[MAX_ENTRIES], float[MAX_ENTRIES], char[MAX_ENTRIES], float[MAX_ENTRIES], int);
int main(void)
{
char firstName[MAX_ENTRIES][MAX_LENGTH], lastName[MAX_ENTRIES][MAX_LENGTH], wageType[MAX_ENTRIES], socials[MAX_ENTRIES][12];
float baseWage[MAX_ENTRIES]{ 0 }, normalPay[MAX_ENTRIES]{ 0 }, overtimePay[MAX_ENTRIES]{ 0 }, totalPay[MAX_ENTRIES]{ 0 };
int hours[MAX_ENTRIES]{ 0 }, entries = 0;
//Define and open all the I/O files
ifstream inputData;
inputData.open("C:\\Users\\jorda\\source\\repos\\cst115-lab8-JordanHT-OIT\\CST116F2021-Lab8\\Data2.txt");
if (!inputData.fail())
{
entries = getInput(inputData, firstName, lastName, socials, baseWage, hours, wageType);
inputData.close();
}
else //Exit if the file cannot be opened
{
cout << endl << "Failed to open data file" << endl;
return (1);
}
for (int idx = 0; idx < entries; idx++)
{
if (hours[idx] > 40)
{
normalPay[idx] = baseWage[idx] * 40.0F;
overtimePay[idx] = baseWage[idx] * 1.5F * (hours[idx] - 40);
}
else
{
normalPay[idx] = baseWage[idx] * hours[idx];
}
totalPay[idx] = overtimePay[idx] + normalPay[idx];
if (wageType[idx] == 'F') //Pay union fees
{
totalPay[idx] -= 5.0F;
}
}
displayInfo(firstName, lastName, socials, baseWage, hours, normalPay, overtimePay, wageType, totalPay, entries);
return (0);
}
//Reads the contents in an input file via a filestream. Returns the number of records read.
int getInput(ifstream& dataFile, char fName[MAX_ENTRIES][MAX_LENGTH], char lName[MAX_ENTRIES][MAX_LENGTH], char SSN[MAX_ENTRIES][12], float baseWage[MAX_ENTRIES], int hours[MAX_ENTRIES], char status[MAX_ENTRIES])
{
int linesRead = 0;
dataFile >> fName[0] >> lName[0] >> SSN[0] >> baseWage[0] >> hours[0] >> status[0];
while (!dataFile.eof())
{
linesRead++;
dataFile >> fName[linesRead] >> lName[linesRead] >> SSN[linesRead] >> baseWage[linesRead] >> hours[linesRead] >> status[linesRead];
}
return (linesRead);
}
//Displays all relevant employee payment information.
void displayInfo(char fName[MAX_ENTRIES][MAX_LENGTH], char lName[MAX_ENTRIES][MAX_LENGTH], char SSN[MAX_ENTRIES][12], float baseWage[MAX_ENTRIES], int hours[MAX_ENTRIES], float normalPay[MAX_ENTRIES], float overtimePay[MAX_ENTRIES], char status[MAX_ENTRIES], float totalPay[MAX_ENTRIES], int entries)
{
cout << left;
cout << "Payment overviewer MK1\n\n" << setw(MAX_LENGTH) << "First name" << setw(MAX_LENGTH) << "Last name" << setw(16) << "SSN" << setw(8) << "Wage" << setw(8) << "Hours" << setw(20) << "Straight-time pay" << setw(16) << "Overtime Pay" << setw(12) << "Status" << setw(16) << "Total pay" << endl;
for (int idx = 0; idx < (MAX_LENGTH * 2 + 90); idx++) //Adjust spacing as needed
{
cout << char(205);
}
cout << endl;
for (int idx = 0; idx < entries; idx++)
{
cout << setw(MAX_LENGTH) << fName[idx];
cout << setw(MAX_LENGTH) << lName[idx];
cout << setw(16) << SSN[idx]; //Adjust spacing as needed
cout << fixed << setprecision(2) << setw(8) << baseWage[idx];
cout << setw(8) << hours[idx];
cout << fixed << setprecision(2) << setw(20) << normalPay[idx];
cout << fixed << setprecision(2) << setw(16) << overtimePay[idx];
if (status[idx] == 'F')
{
cout << setw(12) << "Fulltime";
}
else
{
cout << setw(12) << "Parttime";
}
cout << fixed << setprecision(2) << setw(16) << totalPay[idx] << endl;
}
}
|