Question
PLEASE HELP USING C++ Open up a project in Code::Blocks (whatever name you choose). Next, go into the lab content on Springboard and download the
PLEASE HELP USING C++
Open up a project in Code::Blocks (whatever name you choose). Next, go into the lab content on Springboard and download the program called CSLab09. This you can cut and paste into your main, and use for this exercise. You also will need to download the salaries.txt file. This is the file your program will read and process.
Our practice will involve the writing of an integrated error handling function for our sample program. The program reads a file that contains salary amounts, applies a cost of living adjustment, and then writes the updated amounts to a new salary file. This program also provides a good example of many file operation methods that we have recently been studying. You may run the program to see what it produces. It already has the coding for detecting the errors, and renders a listing of all the new updates with a total at the end. Our error handling function, aptly named errorHandler, will be called based on a prescribed list of possible errors. The function prototype and header are already in place. It accepts the status, a message, and the offending input. The function should write information to the standard error stream. The standard error stream is the default destination for error messages. Its quite easy to use. Rather than using cout, we simply use cerr. You also need a #include of the
What exception processing should do is guarantee that errors always leave your program in a valid state. What real-life applications have to do is guarantee that the final state is either the original state (if there was an error the operation was rolled back) or the intended end state (if there was no error the operation was committed). For file handling in our program, this was easy. The input file was never altered, and the output was default, which says create a new one each time we run the program. For other programs out there in the world you can imagine there are much deeper complexities, but this is what error handling is all about and what makes programming so interesting.
Make sure you use standard error (cerr) to output error messages. Dont forget the #include directive for that. This output is only performed in the errorHandler function. Pass the function the amount, the status, and an informative message (be creative). In the program, look for the comments PLACE ERROR HANDLING HERE. This is where you will want to interface with (call) the error handler. To simplify this we will use string literals for messages, and numeric literal for status as the arguments for the function call. For input data, the item in error, we will pass either the bad data (amount) or the file name experiencing a problem. There are seven calls to the interface. Status codes: ? status code one (1) - any invalid amounts (amounts of zero included) are reported exceptions and ignored (not written to the output file) ? status code two (2) - any file operation errors, such as a failed open, reading and writing, possible corruption, etc these should halt the program. In the error handling function, replace your code here with all of the logic to process the incoming errors. Consider possible ways to gracefully terminate (do not use exit). Make sure that you test your program. Make sure to use good programming practices as described in our standards. Follow the outline above and remember to compile as you go; test after each major part of the function is updated with another exception. When completed, submit the main cpp
THE GIVEN CODE:
// Program id: CSLab09.cpp // This is our template for CS Lab 09 // ERROR HANDLING PRACTICE PROGRAM // This program is used for the University of Akron, Department of Computer Science Laboratories // // This program reads a file that contains salary amounts, applies a cost of living adjustment (RATE_INCREASE), // and writes the updated amounts to a new salary file. Simple program and we will upgrade it to handle errors // goal: put error handling into each function and write our own error handler function as well // // First - // Go to the end of the program to find the errorHandler function. This function will process all errors // if the status is one then the function should resume processing after displaying the message. // if the status is two then the function should interrupt the program. // Considerations for interrupting the program include closing the files properly, etc... look at the logic and // think how to return back to main and let the program end there. Do not use EXIT. // // Second - // Find the PLACE ERROR HANDLING HERE comments in each function and write the interface to the error handler. // Format the data in error, the message, and the status code. You can pass these as literals in your function call. // status code one - any invalid amounts (amounts of zero included) or mild file errors // are reported exceptions. They are ignored (not written) as new output // status code two - any severe file operation errors, such as opening, or severe conditions // while reading and writing, should halt the program // Notice the use of stringstream (we will cover this in class) #include
int main() { ifstream salaryFile; // create file input object ofstream newSalaryFile; // create file output object // notice we open the files and connect them to the names here, in the caller (main), as recommended salaryFile.open("salaries.txt"); // open the file and make the function's parameter an ifstream newSalaryFile.open("newsalaries.txt"); // open the file and make the function's parameter an ofstream if(readFile(salaryFile)) { adjustSalary(salaryFile, newSalaryFile); // adjust salary will process the entire salary file } else { // PLACE ERROR HANDLING HERE (status TWO) } salaryFile.close(); newSalaryFile.close(); return 0; } //Precondition: valid open file with salary amount //Postcondition: cost of living adjusted amount //will be written to new salaries file void adjustSalary(ifstream &salaryFile, ofstream &newSalaryFile) { int validCount = 0; string stringAmount = " "; // notice that we are reading the file into a string, and not a float. You could read into a float, our variable amount. // the reason why we did this is because we can only see the invalid characters if it is a string, and we want them for // our error handling output (so the user knows what exactly was bad). ??? Ask about any of this anytime in class. while (!(salaryFile >> stringAmount).eof()) // avoids the isolated eof test from failure (as explained in our lectures) { float amount = 0.00; stringstream stringStreamAmount(stringAmount); // The stringstream class: uses stream insertion operator stringStreamAmount >> amount; // stream string stream into the amount and convert the streamed string into a float if (!isValid(stringAmount)) // is there an invalid value (non-numeric data) (status one) { // PLACE ERROR HANDLING HERE } else if (salaryFile.fail() && !salaryFile.eof()) { // if fail bit was triggered here it may be more severe than invalid data // so we check the bad bit. If on, it's not a data issue and the stream should be considered unusable. if (salaryFile.bad()) { // PLACE ERROR HANDLING HERE (status TWO) } else { // PLACE ERROR HANDLING HERE (status ONE) // handle the error, and need to clear the buffer and reset the flags (status ONE) salaryFile.clear(); salaryFile.ignore(std::numeric_limits
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started