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

Please help with this debug // Debug 12-4 An Inventory class // Extraction opera

ID: 3554394 • Letter: P

Question

Please help with this debug

// Debug 12-4 An Inventory class
// Extraction operator throws exception
// if cost is higher than expected
// The main() function displays the warning
// but takes no other action
#include<iostream>
#include<string>
using namespace std;
class Inventory
{
friend ostream& operator<<(ostream&, const Inventory&);
friend istream& operator >> (istream&, Inventory&);
private:
int itemNum;
double cost;
};
class InventoryException
{
private:
Inventory item;
string errorMessage;
public:
InventoryException(Inventory aitem, string msg);
void showMsg();
};
InventoryException::InventoryException(Inventory aitem)
{
item = aitem;
errorMessage = msg;
}
void showMsg()
{
cout << item << endl << errorMessage << endl;
}
ostream& operator<<(ostream* out, const Inventory& item)
{
out << "Inventory " << itemNum << " Cost $" << cost;
return out;
}
istream& operator >> (istream& in, Inventory& item)
{
const double HIGH = 10000;
cout << "Enter Inventory number ";
in >> item.itemNum;
cout << "Enter cost ";
in >> item.cost
if(item.cost > HIGH)
{
string warning = "Warning: Cost exceeds usual limit";
InventoryException e(item, warning);
throw(e);
}
return in;
}


int main()
{
const int NUM_ITEMS = 4;
Inventory aitem[NUM_ITEMS];
int x;
for(x = 0; x < NUM_ITEMS; ++x)
{
try
{
cout << "Inventory #" << (x+1) << " ";
cin >> aitem[4];
}
catch(InventoryException error)
{
showMsg();
}
}
cout << endl << "Inventory list:" << endl;
for(x = 0; x < NUM_ITEMS; ++x)
cout << anitem[x] << endl;
return 0;
}

Points will be awarded as soon as answer is submited. Thank you

Explanation / Answer

start with your in>>item.item number.

C++ will only recognize what you have described up top. So if you have int itemNum than item.itemNum won't be recognized.

5th line up from bottom you have << endl << "Inventory etc.... I don't think you should have endl before << "inventory list".... doesn't look right. Try those and see how it works.