Can anyone do a quick pogram to show me how to handle those

Can anyone do a quick pogram to show me how to handle those 3 exceptions using exception handling in C++?

Task: The Assignment deals with exception handling in C++. Goal: Design and implement a program that reads data from a (product) file and displays content on the screen. The (product) file contains product information with the following attributes:

1) Product ID (database integer ID)

2) Product Name (Maximum 200 character string)

3) Product Price (decimal) 4)

Company Name (Maximum 200 character string)

The file is a tab-delimited and contains 10 rows of product information. The data may contain errors during the load so the program must do the following when loading the data file:

5) If the product ID is not an integer, an \"Illegal ID format\" exception occurs

6) If the product price is not a decimal, an \"Illegal price format\" exception occurs If the program encounters any of these or other exceptions, the following must occur:

7) The program reports the error to the user during the load

8) The program ignores the data record and continues with loading the rest of the file When all records are loaded, a report of the information loaded is presented:

9) Show the counts of records successfully and unsuccessfully loaded

Solution

PROGRAM CODE:

/*
* main.cpp
*
* Created on: 29-Nov-2016
* Author: kasturi
*/

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

struct product
{
   int prodID;
   string prodname;
   float price;
   string companyName;
}*prod[10];

int main()
{
   ifstream infile(\"input_data.txt\");
   string line;
   int counter = 0;
   while(getline(infile, line))
   {
       istringstream iss(line);
       prod[counter] = new product;

       try {
       if(!(iss>>prod[counter]->prodID))
       {
           throw \"Illegal ID format\";
       }
       if(!(iss>>prod[counter]->prodname))
       {

       }
       if(!(iss>>prod[counter]->price))
       {
           throw \"Illegal price format\";
       }
       if(!(iss>>prod[counter]->companyName))
           {

           }
       counter++;
       }catch (const char* msg) {
       cerr << msg << endl;
       }

   }

for(int i=0; i<counter;i++)
{
   cout<<prod[i]->prodID<<\" \"<<prod[i]->prodname<<\" \"<<prod[i]->price<<\" \"<<prod[i]->companyName<<endl;
}
cout<<\"Number of records loaded: \"<<counter<<endl;
}

INPUT TEXT FILE:

123   Pencil   32.34   Nataraj

234   Paper   10.20   Max

223   Eraser   20.00   Max

345   Sharpner   12.00   Nataraj

543   Gum   23.50   Cosy

276   Sticker   32.00   Max

765   Ruler   25.00   Cello

654   Pen   34.50   Hero

tes   Box   65.00   Cello

385   label   32   Max

OUTPUT:

123 Pencil 32.34 Nataraj

234 Paper 10.2 Max

223 Eraser 20 Max

345 Sharpner 12 Nataraj

543 Gum 23.5 Cosy

276 Sticker 32 Max

765 Ruler 25 Cello

654 Pen 34.5 Hero

385 label 32 Max

Number of records loaded: 9

Illegal ID format

Can anyone do a quick pogram to show me how to handle those 3 exceptions using exception handling in C++? Task: The Assignment deals with exception handling in
Can anyone do a quick pogram to show me how to handle those 3 exceptions using exception handling in C++? Task: The Assignment deals with exception handling in
Can anyone do a quick pogram to show me how to handle those 3 exceptions using exception handling in C++? Task: The Assignment deals with exception handling in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site