Lab 8 Exercises Benjamin Schroeder ###################################################################################################################### 13a 11.7 Exercises p 317 4 pts #5-6 Submit: Statements for 5, Judgements for 6 ########################################### #5 #include #include #include using namespace std; using std::ifstream; using std::ofstream; using std::ios; const int RECORDS = 100; const int MAX = 4; int main() /* { string records; int num_records = 0; ifstream input_file; //define input file ofstream output_file; //define output file char lname[21], id[5]; int age; input_file.open("Sample.txt"); // open the input file output_file.open("Report.txt"); // open the output file if (input_file.is_open() && output_file.is_open()) { input_file >> lname[num_records]<> lname[num_records]< #include #include #include #include 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"); 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"; } //////////////////////////////////////////////// RUN ///////////////////////////////////////// Given List: 15 25 12 14 26 32 52 12 27 44 2 32 Sorted List: 2 12 12 14 15 25 26 27 32 32 44 52 Median: 25.5 C:\Users\Lenovo\Source\Repos\cst115-lab8-BensProgramma\x64\Debug\CST116F2021-Lab8.exe (process 21852) exited with code 0. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . . ############################################################################################################################## 13c 11.13 Debugging Exercises pp 333-336 10 pts Submit: code & runs //// ////////////////////////////// CODE /////////////////////////////////////////// ******************************************************************* * 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 #include // For the files!!!! #include // 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"; } // /////////////////////////////////// RUN ////////////////////////////////// Troy 12 George 4 Kelly 13 ** Total Records: 3 ** The End C:\Users\Lenovo\Source\Repos\cst115-lab8-BensProgramma\x64\Debug\CST116F2021-Lab8.exe (process 21864) exited with code 0. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . . ############################################################################################################################### 13d 11.14 Programming Exercises pp. 336-337 10 pts #1 Submit: code & run 34 pts // ///////////////////////// CODE ////////////////////////////// #include #include #include #include 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 } // ///////////////////////////////// RUN //////////////////////// Name SSN# Wage Hours RegPay OT Status Net Pay John Smith 123-09-9765 $9.00 46 $360.00 $81.00 F $436.00 Molly Brown 432-89-7654 $9.50 40 $380.00 $0.00 F $375.00 Tim Wheeler 239-34-3458 $11.25 83 $450.00 $725.62 F $1170.62 Keil Wader 762-84-6543 $6.50 35 $227.50 $0.00 P $227.50 Trish Dish 798-65-9844 $7.52 40 $300.80 $0.00 P $300.80 Anthony Lei 934-43-9843 $9.50 56 $380.00 $228.00 F $603.00 Kevin Ashes 765-94-7343 $4.50 30 $135.00 $0.00 P $135.00 Cheryl Prince 983-54-9000 $4.65 45 $186.00 $34.88 F $215.88 Kim Cares 343-11-2222 $10.00 52 $400.00 $180.00 F $575.00 Dave Cockroach 356-98-1236 $5.75 48 $230.00 $69.00 F $294.00 Will Kusick 232-45-2322 $15.00 45 $600.00 $112.50 P $712.50 C:\Users\Lenovo\Source\Repos\cst115-lab8-BensProgramma\x64\Debug\CST116F2021-Lab8.exe (process 9572) exited with code 0. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .