//Tyler Taormina //Lab 8 Text file // CST 116 CST116 Module 8: Lab 8 ================================================================================================ 13a 11.7 Exercises p 317 4 pts #5-6 Submit: Statements for 5, Judgements for 6 5. a.) .is_open() b.) int num_data = 0; //open file ifstream input ("lab8.txt"); // check open file if (input.is_open()) { // priming input >> lname[num_data] >> id[num_data] >> age[num_data]; } else cout << "File not opened" << endl; return 0; c.) for the above ^^ input.close(); 6. a. false b. true c. false d. true e. true ================================================================================================ 13b 11.9 Learn by Doing Exercises p 323 10 pts #1: write a full program to call the function Submit: code & run CODE: // Tyler Taormina // CST 116 // Nov 2021 // Lab 8 Exercises #include using namespace std; void findMedian(float arr[], int); float GetValues (float values[]); #define MAX 20 int main () { int num_values; float values [MAX] = {0}; num_values = GetValues(values); cout << "Here is our list..." << endl; for (int i = 0; i < num_values; i++) cout << values[i] << " "; cout << endl; cout << "===================================================" << endl; findMedian(values, num_values); return 0; } float GetValues (float values[]) { int num_values = 0; char cont = 'n'; do { cout << "Enter a number: "; cin >> values[num_values++]; cout << "Enter another values (y/n)? "; cin >> cont; } while (toupper (cont) == 'Y' && num_values < MAX); return num_values; } void findMedian(float arr[], int num_val) { float x; if ((num_val % 2) == 0) { x = (arr[num_val/2] + arr[(num_val/2) - 1])/2; cout << "Even number Array. We will average the two middle most numbers to determine the median." << endl; cout << "The median is: " << x << endl; } else { cout << "Odd number Array. We will find the number in the middle of the list which determines the median." << endl; cout << "The median is: " << arr[num_val/2] << endl; } } RUN: Enter a number: 1 Enter another values (y/n)? y Enter a number: 2 Enter another values (y/n)? y Enter a number: 3 Enter another values (y/n)? y Enter a number: 4 Enter another values (y/n)? n Here is our list... 1 2 3 4 =================================================== Even number Array. We will average the two middle most numbers to determine the median. The median is: 2.5 till-t@Air:~/code/cst115-lab8-till-t|master⚡ ⇒ ./a.out Enter a number: 1 Enter another values (y/n)? y Enter a number: 2 Enter another values (y/n)? y Enter a number: 3 Enter another values (y/n)? n Here is our list... 1 2 3 =================================================== Odd number Array. We will find the number in the middle of the list which determines the median. The median is: 2 ================================================================================================ 13c 11.13 Debugging Exercises pp 333-336 10 pts Submit: code & runs CODE: #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::resetiosflags; using std::setiosflags; 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 ( "debug_report.txt" ); inFile.open ( "debug.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: outFile"; cout << "\n\n\t\t ** About to EXIT NOW! ** "; } } else { cout << "Trouble Opening File: inFile"; 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: ***output file is included in debug exercise folder in github*** tyler 27 taury 26 ollie 1 ryan 25 elayna 15 mom 40 ** Total Records: 6 ** The End ================================================================================================ 11.14 Programming Exercises pp. 336-337 10 pts #1 Submit: code & run CODE: #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::resetiosflags; using std::setiosflags; using std::ifstream; using std::ofstream; const int EMPLOYEES = 20; const int MAX = 21; int ReadData ( ifstream &inFile, char name[][MAX], char lname [][MAX], char social[][MAX], float wage[], float hours[], char status[]); void PrintTotalsAndSummary (char name[][MAX], char lname [][MAX], char social[][MAX], float wage[], float hours[], char status[], float pay[], float Opay[], float NetPay[], int record_counter); int main ( ) { char name[EMPLOYEES][MAX]; char lname[EMPLOYEES][MAX]; char social[EMPLOYEES][MAX]; float wage[EMPLOYEES]; float hours[EMPLOYEES]; float pay[EMPLOYEES]; float Opay[EMPLOYEES]; float NetPay[EMPLOYEES]; char status[EMPLOYEES]; int record_counter ( 0 ); ifstream inFile; // Notice how this automatically opens the file inFile.open ( "bus.txt"); if ( inFile.is_open ( ) ) { record_counter = ReadData (inFile, name, lname, social, wage, hours, status); inFile.close ( ); } else { cout << "Trouble Opening File: inFile"; cout << "\n\n\t\t ** About to EXIT NOW! ** "; } PrintTotalsAndSummary (name, lname, social, wage, hours, status, pay, Opay, NetPay, record_counter); return 0; } int ReadData ( ifstream & inFile, char name[][MAX], char lname [][MAX], char social[][MAX], float wage[], float hours[], char status[]) { int counter = 0; inFile >> name[counter] >> lname[counter] >> social[counter] >> wage[counter] >> hours[counter] >> status[counter] ; // Priming Read while ( !inFile.eof ( ) ) { ; counter++; inFile >> name[counter] >> lname[counter] >> social[counter] >> wage[counter] >> hours[counter] >> status[counter] ; } return counter; } void PrintTotalsAndSummary (char name[][MAX], char lname [][MAX], char social[][MAX], float wage[], float hours[], char status[], float pay[], float Opay[], float NetPay[], int record_counter) { int i; int reg_hours; int o_hours; float o_wage; for (i = 0; i < record_counter; i++) { // overtime hours and pay if (hours[i] > 40) { o_hours = hours[i] - 40; o_wage = (wage[i] / 2) + wage[i]; Opay[i] = o_hours * o_wage; pay[i] = wage[i] * 40; NetPay[i] = pay[i] + Opay[i]; if (status[i] == 'F') NetPay[i] -= 5.00; } // normal hours and pay else { pay[i] = wage[i] * hours[i]; NetPay[i] = pay[i]; Opay[i] = 0; if (status[i] == 'F') NetPay[i] -= 5.00; } } cout << setiosflags ( ios::left ) << setw (10) << "Name" << resetiosflags ( ios::left ) << setw(20) << "Last Name" << resetiosflags (ios::left) << setw(20) << "Social" << resetiosflags (ios::left) << setw(20) << "Wage" << resetiosflags (ios::left) << setw(20) << "Hours" << resetiosflags (ios::left) << setw(20) << "Regular Pay" << resetiosflags (ios::left) << setw(20) << "Overtime Pay" << resetiosflags (ios::left) << setw(20) << "Status" << resetiosflags (ios::left) << setw(20) << "Net Pay" << resetiosflags (ios::left) << endl; cout << "===========================================================================================================================================================================" << endl; for (int j = 0; j < record_counter; j++) { cout << setiosflags ( ios::left ) << setw (10) << name[j] << resetiosflags ( ios::left ) << setw(20) << lname[j] << resetiosflags (ios::left) << setw(20) << social[j] << resetiosflags (ios::left) << setw(20) << wage[j] << resetiosflags (ios::left) << setw(20) << hours[j] << resetiosflags (ios::left) << setw(20) << pay[j] << resetiosflags (ios::left) << setw(20) << Opay[j] << resetiosflags (ios::left) << setw(20) << status[j] << resetiosflags (ios::left) << setw(20) << NetPay[j] << resetiosflags (ios::left) << endl; } } RUN: till-t@Air:~/code/cst115-lab8-till-t/11.14_exercise|master⚡ ⇒ ./a.out Name Last Name Social Wage Hours Regular Pay Overtime Pay Status Net Pay =========================================================================================================================================================================== John Smith 123-09-8765 9 46 360 81 F 436 Molly Brown 432-89-7654 9.5 40 380 0 F 375 Tim Wheeler 239-34-3458 11.25 83 450 725.625 F 1170.62 Keil Wader 762-84-6543 6.5 35 227.5 0 P 227.5 Trish Dish 798-65-9844 7.52 40 300.8 0 P 300.8 Anthony Lei 934-43-9843 9.5 56 380 228 F 603 Kevin Ashes 765-94-7343 4.5 30 135 0 P 135 Cheryl Prince 983-54-9000 4.65 45 186 34.875 F 215.875 Kim Cares 343-11-2222 10 52 400 180 F 575 Dave Cockroach 356-98-1236 5.75 48 230 69 F 294 Will Kusick 232-45-2322 15 45 600 112.5 P 712.5