Question: Please answer in full main.cpp c++ file only 17.7 Lab: Exception Handling Running the below program with the given input causes an error when extracting

Please answer in full main.cpp c++ file only

17.7 Lab: Exception Handling

Running the below program with the given input causes an error when extracting an integer from a stringstream. The program reads from cin the following rows (also called records) that contain a last name, first name, department, and annual salary. The program uses the stringstream to convert the last entry for the salary to an integer.

Doe,John,Operations,50000 Doette,Jane,Marketing,sixty_thousand Uminum,Al,Finance,70000 Jones,Ellen,Sales,80000 

Note that the second row has a value that is type string, not type int, which will cause a problem.

Run the program and note the program fails and throws an ios_base::failure exception.

At FIXME: Add try/catch statements to catch the ios_base::failure exception. In this case, print a message, and do not add the item to the total salaries.

Ex: For the string:

Doette,Jane,Marketing,sixty_thousand 

the message should say:

The salary entry for Jane Doette, sixty_thousand, could not be converted to an integer. This info won't be added to the salary running total. 

For the input:

Doe,John,Operations,50000 Doette,Jane,Marketing,sixty_thousand Uminum,Al,Finance,70000 Jones,Ellen,Sales,80000 

the output is:

 John, Doe, 50000, Operations The salary entry for Jane Doette, sixty_thousand, could not be converted to an integer. This info won't be added to the salary running total. Al, Uminum, 70000, Finance Ellen, Jones, 80000, Sales Total salaries: 200000. 

Starting Code:

#include #include #include #include #include using namespace std;

int main() { // Describe the format of a row of input. There are four fields in // a row separated by commas: last name, first name, department, salary const string SEPARATOR = ","; // field separator in each row of data const int INDEX_LAST_NAME = 0; // # of the last name field const int INDEX_FIRST_NAME = 1; // # of the first name field const int INDEX_DEPT = 2; // # of the department name field const int INDEX_SALARY = 3; // # of the salary field stringstream ss; // For conversion of string to int int salary; vector field; // Fields on one row in the input file string row; // One row of the input file string partial; int nRows; // Counts # of rows in the input file int totalSalaries; // Total of all salaries read

nRows = 0; totalSalaries = 0; ss.exceptions(ios::failbit); // Failed convertion will throw an // ios_base::failure getline(cin, row); while (row.length() > 0) { // Loop while input data exists ++nRows; while(row.find(',') != string::npos) { partial = row.substr(0, row.find(',')); field.push_back(partial); row = row.substr(row.find(',') + 1); } field.push_back(row); // FIXME: Add a try/catch clause to catch an ios_base::failure exception. // Show the user the row that had the problem and indicate // there was a conversion error on the salary.

ss.str(""); // Reset contents of stringstream ss.clear(); // Clear stringstream state ss << field[INDEX_SALARY]; ss >> salary; totalSalaries += salary; cout << " " << field[INDEX_FIRST_NAME] << " " << field[INDEX_LAST_NAME] << ", " << field[INDEX_SALARY] << ", " << field[INDEX_DEPT] << endl; getline(cin, row); field.clear(); } cout << endl; cout << "Total salaries: " << totalSalaries << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!