aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab8/CST116F2021-Lab8.cpp
blob: d7607a5cecab3088959cf34e893eefef1cbbfe58 (plain) (blame)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
////////////////////////////////////////////////
// ANSWERS TO PROBLEM #5 ON PAGE 317 ARE BELOW
////////////////////////////////////////////////


// 5 a. Necessary statement to check to make sure a file is open:
//		fileName.is_open

// 5 b. Necessary statement to read and display all information contained within the space delimited
//			file declared above:
// 
// string readStr;
//
// if (fileName.is_open())
// {
// 	 while (!fileName.eof())
//	 {
//		 getline(fileName, readStr, ' ');
//		 cout << readStr;
//	 }
// }

// 5 c. Necessary statment to close file:
//		fileName.close

// 6 a. FALSE
// 6 b. TRUE
// 6 c. TRUE
// 6 d. TRUE
// 6 e. TRUE


//////////////////////////////////////////////////////////////
// CODE FOR PROBLEM #1 (LEARN BY DOING) ON PAGE 323 IS BELOW
//////////////////////////////////////////////////////////////

//#include <iostream>
//#include <iomanip>
//
//using namespace std;
//
//const int MAX_VALUES{ 25 };
//
//void getValues(int[], int&); //gets user input list of integers
//void calcMedian(int[], int); //finds and displays median of list of integers
//
//int main()
//{
//	int values[MAX_VALUES];
//	int num_values = 0;
//
//	getValues(values, num_values); //gets user input list of integers
//	calcMedian(values, num_values); //finds and displays median of list of integers
//
//}
//
//
//void getValues(int values[], int& num_values) //gets user input list of integers
//{
//	char cont = 'y';
//
//	cout << "\n\t\tEnter up to 25 integers, smallest to greatest\n";
//
//	do
//	{
//		cout << "\n\tInteger " << num_values + 1 << " : ";
//		cin >> values[num_values];
//
//		num_values++;
//
//		cout << "\n\tEnter another? (y/n): ";
//		cin >> cont;
//
//	} while (num_values < MAX_VALUES + 1 && cont == 'y');
//}
//
//void calcMedian(int values[], int num_values) //finds and displays median of list of integers
//{
//	if (num_values % 2 == 1) //if number of values in list is odd
//	{
//		int middle1 = (num_values / 2) + 0.5;
//
//		cout << "\n\t\tThe median is " << values[middle1] << endl;
//	}
//	else //if number of values is even
//	{
//		int middle2 = (num_values / 2) - 1; //left middle number index
//		int middle3 = (num_values / 2); //right middle number index
//		float middleValue = ((values[middle2] + values[middle3]) / 2) + 0.5; //median value between the two middle numbers
//
//		cout << "\n\t\tThe median is " << middleValue << endl;
//	}
//}


/////////////////////////////////////////////////////
// CODE FOR DEBUGGING EXERCISE ON PAGE 333 IS BELOW
/////////////////////////////////////////////////////

/********************************************************************
* 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\\eclip\\source\\repos\\cst115-lab8-JosephTenEyck\\Data Report.txt");
//
//    inFile.open("C:\\Users\\eclip\\source\\repos\\cst115-lab8-JosephTenEyck\\Data File.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 << "Trouble Opening 'Data Report.txt'";
//            cout << "\n\n\t\t ** About to EXIT NOW! ** ";
//        }
//    }
//    else
//    {
//        cout << "Trouble Opening 'Data File.txt'";
//        cout << "\n\n\t\t ** About to EXIT NOW! ** ";
//    }
//    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];
//    }
//
//    cout << setiosflags(ios::left) << setw(25)
//        << name[counter] << resetiosflags(ios::left)
//        << setw(4) << age[counter] << endl;
//
//    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 + 1 << " **\n"
//        << "\t\t The End \n";
//
//    // To file
//    outFile << "\n\n\t** Total Records: " << totalRecords + 1 << " **\n"
//        << "\t\t The End \n";
//}



///////////////////////////////////////////////////////////////////
// CODE FOR PROBLEM #1 PROGRAMMING EXERCISES ON PAGE 336 IS BELOW
///////////////////////////////////////////////////////////////////

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int NUM_EMPLOYEES = 11;
const int MAX = 21;

void readData(ifstream& inFile, char[][MAX], char[][MAX], char[][MAX], float[], int[], char[]); //reads input file into arrays
void calcPay(float[], int[], char[], float[], float[], float[]); //calculates pay information
void displayInfo(char[][MAX], char[][MAX], char[][MAX], float[], int[], char[], float[], float[], float[]); //displays all information in an organized fashion

int main()
{
    //input arrays from file
    char firstName[NUM_EMPLOYEES][MAX];
    char lastName[NUM_EMPLOYEES][MAX];
    char ssNum[NUM_EMPLOYEES][MAX];
    float baseWage[NUM_EMPLOYEES];
    int hoursWorked[NUM_EMPLOYEES];
    char status[NUM_EMPLOYEES];

    //calculated arrays
    float straightPay[NUM_EMPLOYEES];
    float overTimePay[NUM_EMPLOYEES];
    float netPay[NUM_EMPLOYEES];


    ifstream inFile; //delares input file

    ofstream outFile("C:\\Users\\eclip\\source\\repos\\cst115-lab8-JosephTenEyck\\Data Report 2.txt"); //declares output file and opens it

    inFile.open("C:\\Users\\eclip\\source\\repos\\cst115-lab8-JosephTenEyck\\Data File 2.txt"); //opens input file


    if (inFile.is_open())
    {
        readData(inFile, firstName, lastName, ssNum, baseWage, hoursWorked, status); //reads input file into arrays
        inFile.close();
    }

    calcPay(baseWage, hoursWorked, status, straightPay, overTimePay, netPay); //calculates pay information

    displayInfo(firstName, lastName, ssNum, baseWage, hoursWorked, status, straightPay, overTimePay, netPay); //displays all information in an organized fashion

        return 0;
}

void readData(ifstream& inFile, char firstName[][MAX], char lastName[][MAX], char ssNum[][MAX], float baseWage[], int hoursWorked[], char status[]) //reads input file into arrays
{
    int record_count = 0;

    inFile >> firstName[record_count] >> lastName[record_count] >> ssNum[record_count]
        >> baseWage[record_count] >> hoursWorked[record_count] >> status[record_count]; //priming read

    while (!inFile.eof())
    {
        record_count++;

        inFile >> firstName[record_count] >> lastName[record_count] >> ssNum[record_count]
            >> baseWage[record_count] >> hoursWorked[record_count] >> status[record_count]; //inputs the rest of the file into the arrays
    }
}

void calcPay(float baseWage[], int hoursWorked[], char status[], float straightPay[], float overTimePay[], float netPay[]) //calculates pay information
{
    int hoursOver = 0;
    float actualWage = 0;

    for (int i = 0; i < NUM_EMPLOYEES; i++)
    {
        if (status[i] == 'F') //check for full-time status
        {
            actualWage = baseWage[i] - 5;
        }
        else
        {
            actualWage = baseWage[i];
        }

        if (hoursWorked[i] > 40) //if overtime worked
        {
            hoursOver = hoursWorked[i] - 40;

            straightPay[i] = (40 * actualWage);
            overTimePay[i] = (hoursOver * (3/2) * actualWage);
            netPay[i] = straightPay[i] + overTimePay[i];
        }
        else //overtime not worked
        {
            straightPay[i] = (hoursWorked[i] * actualWage);
            overTimePay[i] = 0;
            netPay[i] = straightPay[i];
        }
    }
}

void displayInfo(char firstName[][MAX], char lastName[][MAX], char ssNum[][MAX], float baseWage[], int hoursWorked[], 
    char status[], float straightPay[], float overTimePay[], float netPay[]) //displays all information in an organized fashion
{
    cout << "\n\t\t========================";
    cout << "\n\t\t  EMPLOYEE INFORMATION";
    cout << "\n\t\t========================";

    cout << "\n\n                                     Base   Hours  Full/Part    Straight      Over ";
    cout << "\nName                 SS#             Wage   Worked   Time       Time Pay    Time Pay    Net Pay";
    cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

    for (int i = 0; i < NUM_EMPLOYEES; i++) //includes specific name spacing for this specific data
    {
        cout << setw(7) << left << firstName[i] << " " << setw(13) << left << lastName[i] << setw(15) << left << ssNum[i]
            << "$" << setw(5) << left << baseWage[i] << "    " << setw(8) << left << hoursWorked[i] << setw(11) << left << status[i]
            << "$" << setw(11) << left << straightPay[i] << "$" << setw(11) << left << overTimePay[i] << "$" << setw(11) << left << netPay[i] << endl;

          //cout << setw(7) << left << firstName[i];
    }
}