aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab8/CST116F2021-Lab8_Schroeder.cpp
blob: 9abcef51a0c82841d099849ff210b1f5e93bf9a3 (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// CST116F2021-Lab8_Schroeder.cpp : This file contains the 'main' function. Program execution begins and ends there.

//  //////////////////        11.7 Exercises   SHOWN IN LAB_8_CODE_RUN  .txt FILE  /////////////////

//  //////////////////////        11.9 Learn By Doing Exercise   pp323          ////////////////////////
/*
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stdio.h>
using namespace std;
const int RECORDS = 100;
const int MAX = 4;
int main()
{
    int records[RECORDS]{};
    int num_records = 0;
    ifstream inFile;
    FILE* inPtr;
    // open the file
    //fopen_s(&inPtr, "C:\\Users\\Lenovo\\source\\repos\\cst115-lab8-BensProgramma\\CST116F2021-Lab8\\11_13_Average_Data.txt", "r");
    fopen_s(&inPtr, "11_13_Average_Data.txt", "r");
    int inInt;
    // read the file
    if (inPtr != 0)
    {
        if (inPtr != NULL)
        {
            int j = 0;
            while (fscanf_s(inPtr, "%i", &inInt) >= 0)
            {
                records[num_records] = inInt;
                num_records++;
                j++;
            }
        }
    }
    else
        cout << "Error:   The File can not be opened";

    // close the file
    fclose(inPtr);

    cout << "Given List:\n";
    for (int r = 0; r < num_records; r++)
    {
        cout << records[r] << " ";
    }
    // Sort the numbers smallest to largest
    int j = 0, k = 0, temp = 0;
    for (j = 0; j < num_records; j++)
    {
        for (k = 0; k < num_records - 1; k++)
        {
            if (records[k] > records[k + 1])
            {
                temp = records[k];
                records[k] = records[k + 1];
                records[k + 1] = temp;
            }
        }
    }
    cout << "\nSorted List:\n";
    for (int r = 0; r < num_records; r++)
    {
        cout << records[r] << " ";
    }


    // Find the median of the list
    float median = 0;
    // if number of elements are even
    if (num_records % 2 == 0)
        median = (records[(num_records - 1) / 2] + records[num_records / 2]) / 2.0;
    // if number of elements are odd
    else
        median = records[num_records / 2];
    cout << "\nMedian: " << median << "\n";
}

*/


// ///////////////////////        11.13 Debugging Exercises     pp 333 - 336     /////////////////////
/*
*******************************************************************
* 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("Chap_11_Report2.txt");

    inFile.open("Chap_11_data2.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 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, 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";
}

*/





// ////////////////////////        11.14 Programming Exercise   pp336 -337       //////////////////////
/*
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{

    char first[100]{};
    char last[100]{};
    char ssn[12]{};
    float wage =0;
    int hours = 0;
    char status;
    int count = 1;
    int num_records = 0;
    
    float dues = 0.00;
    int OThours = 0;
    float STPay = 0.00;
    float OTPay = 0.00;
    float NetPay = 0.00;

    ifstream fin;
    //fin.open("C:\\Users\\Lenovo\\source\\repos\\cst115-lab8-BensProgramma\\CST116F2021-Lab8\\11_14_Data.txt");
    fin.open("11_14_Data.txt");   //open the file

    cout << "   Name\t\t    SSN#\tWage\t      Hours\tRegPay\t\t  OT\t      Status\tNet Pay\n" << endl;   //Output Table Header

    while (fin && !fin.eof())
    {
        fin >> first;           //  Read the file
        fin >> last;
        fin >> ssn;
        fin >> wage;
        fin >> hours;
        fin >> status;
        num_records++;

        OThours = 0;           //Calculate Overtime if needed
        if (hours> 40)
        {
            OThours = hours - 40;
            hours = hours - OThours;
        }
        STPay = wage * hours;
        OTPay = wage * (1.5 * OThours);
        hours = hours + OThours;

        dues = 5.00;            //Calculate Union Dues
        if (status=='P')
        {
            dues = 0.00;
        }

        NetPay = STPay + OTPay - dues;          //Calculate Net Pay


        if ( num_records<12)            //Print out Table
        {
            cout << first << " "
                << last << "\t"
                << ssn << "\t$"
                << fixed << setprecision(2)<< wage << "\t\t"
                << hours << "\t$"
                << fixed << setprecision(2) << STPay << "\t\t$"
                << fixed << setprecision(2) << OTPay << "\t\t"
                << status << "\t"
                << "$" << fixed << setprecision(2) << NetPay << endl;
            count++;

        }    

    }

    fin.close();            //Close the file
}

*/













// ////////////////////     In Class example    ////////////////////////////////////
    // R-WFiles.cpp  using getline
    /*
    int main()
    {
        /*
        string records[RECORDS][MAX];    //2 dimensional string array
        int num_records = 0;
        ifstream inFile;
        ofstream outFile;
        inFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Data.txt");        // open the input file
        outFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Report.txt");    // open the output file

        if (inFile.is_open() && outFile.is_open())        //   LINE BY LINE MANIPULATION
        {
            while (getline(inFile, records[num_records][0], ' '))  //reads the first item in the file
            {
                cout << records[num_records][0] << ' ';    //print out to screen
                outFile << records[num_records][0] << ' ';   //print to the output file
                getline(inFile, records[num_records][1], ' ');
                cout << records[num_records][1] << ' ';    //print out to screen
                outFile << records[num_records][1] << ' ';   //print to the output file
                cout << records[num_records][2] << ' ';    //print out to screen
                outFile << records[num_records][2] << ' ';   //print to the output file
                getline(inFile, records[num_records][3], ' ');
                cout << records[num_records][3] << ' ';    //print out to screen
                outFile << records[num_records][3] << ' ';   //print to the output file
                num_records++;


            }

        }
        */
    // R-WFiles.cpp  using fscanf
    /*
        string records[RECORDS][MAX]{};    //2 dimensional string array
        int num_records = 0;
        char inChar[50];
        string inString;

        FILE* inPtr;     // file pointer
        ifstream inFile;
        ofstream outFile;
        //inFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Data.txt");        // open the input file
        fopen_s(&inPtr, "C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Data.txt","r");
        outFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Report.txt");    // open the output file


        if (inPtr != NULL && outFile.is_open()) //check that there is stuff to read       //   SAFER WAY TO READ THINGS IN  fscan
        {
            while (fscanf_s(inPtr,"%s", inChar, 49) >=0)  //reads the first item in the file, tells how big the space is.  left space for null character
            {
                //inString = inChar;   //  convert it to a string  and inserts the NULL character
                records[num_records][0] = inChar;
                cout << records[num_records][0] << ' '; //print out to screen
                outFile << records[num_records][0] << ' ';   //print to the output file

                fscanf_s(inPtr,"%s", inChar, 49);
                //inString = inChar;   //  convert it to a string  and inserts the NULL character
                records[num_records][1] = inChar;
                cout << records[num_records][1] << ' ';    //print out to screen
                outFile << records[num_records][1] << ' ';   //print to the output file

                fscanf_s(inPtr,"%s", inChar, 49);
                //inString = inChar;   //  convert it to a string  and inserts the NULL character
                records[num_records][2] = inChar;
                cout << records[num_records][2] << ' ';    //print out to screen
                outFile << records[num_records][2] << ' ';   //print to the output file

                fscanf_s(inPtr,"%s", inChar, 49);
                //inString = inChar;   //  convert it to a string  and inserts the NULL character
                records[num_records][3] = inChar;
                cout << records[num_records][3] << ' ';    //print out to screen
                outFile << records[num_records][3] << ' ';   //print to the output file

                num_records++;
            }

        }

        fclose(inPtr);   // close input file
        outFile.close();  // close output file


        return 0;

    }
    */
    // R-WFiles.cpp : fscanf_s reading strings
    /*
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <stdio.h>
    using namespace std;
    const int RECORDS = 100;
    const int MAX = 4;
    int main()
    {
        string records[RECORDS][MAX]{};
        int num_records = 0;
        ifstream inFile;
        ofstream outFile;
        outFile.open("C:\\TEMP\\Chap_11_Report.txt");
        FILE* inPtr;
        char inChar[50]{};
        string inString;
        if (fopen_s(&inPtr, "C:\\TEMP\\Chap_11_Data.txt", "r") == 0)
        {
            if (inPtr != NULL && outFile.is_open())
            {
                while (fscanf_s(inPtr, "%s", inChar, 49) >= 0)
                {
                    //inString = inChar;
                    records[num_records][0] = inChar;
                    cout << records[num_records][0];
                    outFile << records[num_records][0];
                    fscanf_s(inPtr, "%s", inChar, 49);
                    //inString = inChar;
                    records[num_records][1] = inChar;
                    cout << records[num_records][1];
                    outFile << records[num_records][1];
                    fscanf_s(inPtr, "%s", inChar, 49);
                    //inString = inChar;
                    records[num_records][2] = inChar;
                    cout << records[num_records][2];
                    outFile << records[num_records][2];
                    fscanf_s(inPtr, "%s", inChar, 49);
                    //inString = inChar;
                    records[num_records][3] = inChar;
                    cout << records[num_records][3];
                    outFile << records[num_records][3];
                    num_records++;
                }
            }
        }
        outFile.close();
        fclose(inPtr);
    }
    */
    // R-WFiles.cpp : fscanf_s reading ints
    /*
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <stdio.h>
    using namespace std;
    const int RECORDS = 100;
    const int MAX = 4;
    int main()
    {
        int records[RECORDS]{};
        int num_records = 0;
        ifstream inFile;
        ofstream outFile;
        outFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\11_13_MedianReport.txt");
        FILE* inPtr;
        fopen_s(&inPtr,"C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\11_13_Average_Data.txt","r");
        int inInt;

        if (inPtr != 0)
        {
            if (inPtr != NULL && outFile.is_open())
            {
                int j = 0;
                while (fscanf_s(inPtr, "%i", &inInt) >= 0)
                {
                    records[num_records] = inInt;
                    //cout << records[num_records]<< " ";
                    outFile << records[num_records]<< " ";
                    num_records++;
                    j++;
                }
            }
        }
        else
            cout << "Error:   The File can not be opened";


        outFile.close();
        fclose(inPtr);

        cout << "given list:\n";
        for (int r = 0; r < num_records; r++)
        {
            cout << records[r] << " \n";

        }
        // Sort the numbers smallest to largest
        int j = 0, k = 0, temp = 0;
        for (j = 0; j < num_records; j++)
        {
            for (k = 0; k < num_records - 1; k++)
            {
                if (records[k] > records[k + 1])
                {
                    temp = records[k];
                    records[k] = records[k + 1];
                    records[k + 1] = temp;
                }
            }
        }

        cout << "\n sorted list:\n";
        for (int r = 0; r < num_records; r++)
        {
            cout << records[r] << " \n";
        }
    }
    */
    // R-WFiles.cpp : getline ints
    /*
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <stdio.h>
    using namespace std;
    const int RECORDS = 100;
    const int MAX = 4;
    int main()
    {
        int records[RECORDS]{};
        int num_records = 0;
        ifstream inFile;
        ofstream outFile;
        outFile.open("C:\\TEMP\\Chap_11_intReport.txt");
        inFile.open("C:\\TEMP\\Chap_11_intData.txt");
        string inString;
        if (inFile.is_open() && outFile.is_open())
        {
            while (getline(inFile, inString, ','))
            {
                records[num_records] = stoi(inString);   //string to int
                outFile << records[num_records];
                cout << records[num_records];
                num_records++;
            }
        }

        outFile.close();
        inFile.close();
    }

    */
    // R-WFiles.cpp : cin/cout reading files
    /*
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    using namespace std;
    const int RECORDS = 100;
    const int MAX = 4;
    int main()
    {
        int records[RECORDS];
        int num_records = 0;
        ifstream inFile("C:\\TEMP\\Chap_11_intData.txt");
        ofstream outFile("C:\\TEMP\\Chap_11_intReport.txt");
        streambuf* cinbuf = cin.rdbuf();
        streambuf* coutbuf = cout.rdbuf();
        cin.rdbuf(inFile.rdbuf());
        cout.rdbuf(outFile.rdbuf());
        while (cin >> records[num_records])
        {
            cout << records[num_records] << ' ';
            num_records++;
        }
        cin.rdbuf(cinbuf);
        cout.rdbuf(coutbuf);
    }
    */