In C Running the below program with the given input causes a
In C++ 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. Argon,John,Operations,50000 Williams,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. 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. Run the program again and note the total salaries excludes the row with the error.
#include <iostream>
 #include <vector>
 #include <string>
 #include <sstream>
 #include <stdexcept>
 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 = 0;
 
 vector<string> field; // Fields on one row in the input file
 string row; // One row of the input file
 string partial;
 int nRows = 0; // Counts # of rows in the input file
 int totalSalaries = 0; // Total of all salaries read
 
 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;
 }
Solution
try
{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;
}
catch(\"illegal exception,cannot convert to int\");
}


