Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to read the third column from a file to catch the error using except

ID: 3572431 • Letter: I

Question

I am trying to read the third column from a file to catch the error using exception Handling in C++.

Let's say this is the file:

123   Laptop       1000.00   HP

124   Monitor       250.00   Dell

125   Textbook   49.99   Deitel

126a   Tshirt       19.99   Macys

127   Glass       10.00   GlassCo

128   Candy       TBD   Hersheys <--- Trying to catch this error this Price

129   iPhone       250.00   Apple

130   Pants       79.99   Express

131   Chips       4.29   Ruffles

132   Keyboard   29.99   Apple

1.)How would I go about catching the TBD Price?

I have already caught the ID mistake. and the program can count the 1 mistake in the file.

2.) How would I get the average price ?

i tried this:

double averagePrice = 0.0;

vector<Product>products;
       for (unsigned int i = 0; i < products.size(); i++)
       {
           averagePrice += products.at(i).price;
       }
       averagePrice /= products.size();

       cout << "Average price: " << averagePrice << endl;

Help would be immensele appreciated.

Explanation / Answer

you can define your own type of exception using methods like

your average logic looks good other than casting problem into double

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote