// 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 #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"); 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 #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"; } */ // //////////////////////// 11.14 Programming Exercise pp336 -337 ////////////////////// /* #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 } */ // //////////////////// 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 #include #include #include #include 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 #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; 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 #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; 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 #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("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); } */