//////////////////////////////////////////////// // 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 //#include // //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 //#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("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 #include #include 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]; } }