Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 header. If the status is one (1) then the function should resume processing after displaying the message. If the status is two (2) then the function should interrupt the program. Considerations for interrupting the program should include closing the files properly, etc... so you will need to figure out how to leave the program gracefully. Dont use EXIT.

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 #include #include #include #include using std::cout; using std::cin; using std::endl; using std::stringstream; using std::string; using std::ifstream; using std::ofstream; const float RATE_INCREASE = .025; // Function prototypes void adjustSalary(ifstream &, ofstream &); bool readFile(ifstream &); bool writeFile(ofstream &, float &); bool isValid(const string &); void errorHandler(string, int, string); // we can't pass by reference b/c we're using literals

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::max(), ' '); } } else if (amount <= 0) // invalid amount error of zero or less (status ONE) { // PLACE ERROR HANDLING HERE } else if (amount > 999999.99) // invalid amount error over a million (status ONE) { // PLACE ERROR HANDLING HERE } else // trailing else ... everything was valid. { amount = amount + (amount * RATE_INCREASE); if (writeFile(newSalaryFile, amount)) { cout << "New rate: " << std::right << std::setw(15) << std::fixed << std::setprecision(2) << amount << endl; } else // write error to file (status TWO) { // PLACE ERROR HANDLING HERE } validCount++; } } cout << "The number of valid salary adjustments made was: " << validCount << endl; } //*************************************************** // the read file function * //*************************************************** //Precondition: opened file object is passed into this function //Postcondition: The status of the file's disposition will be returned bool readFile(ifstream &salaryFile) { if (!salaryFile.is_open()) { return false; } return true; } //*************************************************** // the write file function * //*************************************************** //Precondition: opened file object is passed into this function and written to //Postcondition: The status of the file's disposition will be returned bool writeFile(ofstream &newSalaryFile, float &newAmt) { newSalaryFile << newAmt << endl; // writing to the output file if (!newSalaryFile.is_open()) { return false; } return true; } //*************************************************** // the is valid function * //*************************************************** //Precondition: a float or double is passed and converted to a string value //Postcondition: The status of the float or double will be returned // as bool (true or false), is it a numeric item or not // this reads the string as an array and looks at each character, one by one bool isValid(const string &str) { for (int i = 0; str[i] != '\0'; i++) { if(!isdigit(str[i]) && str[i] != '.') { return false; } } return true; } //*************************************************** // the error handler function * //*************************************************** //Precondition: an error message and an error status code //Postcondition: The message will display to standard error // the status code will determine if the program ends or returns // The parameters are: // 1) dataInfo is the data that caused the problem // - for amounts, use the stringAmount variable // - for files, use the name of the file // 2) status is 1 or 2 // 3) message is a formatted, informative message void errorHandler(string dataInfo, int status, string message) { // your code here }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logic In Databases International Workshop Lid 96 San Miniato Italy July 1 2 1996 Proceedings Lncs 1154

Authors: Dino Pedreschi ,Carlo Zaniolo

1st Edition

3540618147, 978-3540618140

More Books

Students also viewed these Databases questions

Question

Define the term Working Capital Gap.

Answered: 1 week ago

Question

identify current issues relating to equal pay in organisations

Answered: 1 week ago