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

Lab 4 50 points (Note: This is considered a challenging lab) Purpose: To practic

ID: 3722634 • Letter: L

Question

Lab 4 50 points (Note: This is considered a challenging lab) Purpose: To practice using character and structures and binary files For this lab, please write a program that uses a structure to store the following inventory data in a file: (2 points for each item 2 x 4-6 total points) 1. Item Description 2. Quantity on Hand 3. Wholesale Cost You must use the driver program below and you should not add anything to main. Just write the code for: 1. addRecord 2. viewRecord 3. changeRecord (10 points) (8 points) (10 points) Input validation for Item Quantity and Cost (4 X 3 = 12 points) Must be numeric 1. 2. Must not be negative Must be greater than zero (4 points each) (4 points each) (4 points each) 3. There is no validation for Item Description. File specifications: (2 points) You should provide a test file (with reasonable inventory names) that you have used to test your program The test file should have at least 5 entries and have an extension of ".dat” The file should be located in "C:temp" The file format MUST be binary On Exit: (2 Points. NOTE: this requirement is not represented on the sample output below) Print to the screen: "Thank you for visiting

Explanation / Answer

struct Inventory
{
char desc[30];
int qty;
double wholeSaleCose;
int getqty(){
return qty;
}
};


void addRecord(fstream& inventoryFile)
{ Inventory i = new Inventory(); // creating new struct object to write to file
inventoryFile.open("Inventory.dat", ios::out); //opening file in input mode
cout<<Enter inventory details;
//get the data from user
cout<<" enter description ";
gets(desc);
label1: cout<<" enter qty";
cin>>qty;
if( qty < 0 || isalpha(qty) )
{
cout<<" quantity can be only numbers and cannot be negative";
goto label1;
}
label 2: cout<<" enter cost ";
cin>>wholeSaleCost;
if( wholeSaleCost < 0 || isalpha( wholeSaleCost) )
{
cout<<" cost can be only numbers and cannot be negative";
goto label2;
}

// write whole inventory object to file
inventoryFile.write((char*)&i, sizeof(i)) ;
inventoryFile.close();
}


void viewRecord(fstream& inventoryFile)
{ Inventory j = new Inventory();
inventoryFile.open("Inventory.dat", ios::in);
while(!inventoryFile.eof())
{
inventoryFile.read( (char*) &j, sizeof(j));
cout<<" Desc : "<<j.desc<<" qty : "<<j.qty<<" wholeSaleCost : "<<j.wholeSaleCost;
}
inventoryFile.close();
}

void changeRecord(fstream& inventoryFile)
{
Inventory i = new Inventory();
inventoryFile.open("Inventory.dat", ios::out | ios::in);
int qty;
cout<<"Enter the quantity of the record to be changed"; // yoy can compare on the basis of anything qty or cost
cin>>qty;
while(!inventoryFile.eof())
{
inventoryFile.read( (char*) &i, sizeof(i));
if(qty == i.getqty()){
cout<<" Enter new record details";
cout<<" enter description ";
gets(i.desc);
label1: cout<<" enter qty";
cin>>i.qty;
if(i.qty < 0 || isalpha(i.qty) )
{
cout<<" quantity can be only numbers and cannot be negative";
goto label1;
}
label 2: cout<<" enter cost ";
cin>>i.wholeSaleCost;
if( i.wholeSaleCost < 0 || isalpha( i.wholeSaleCost) )
{
cout<<" cost can be only numbers and cannot be negative";
goto label2;
}

// write new inventory record to file
inventoryFile.write((char*)&i, sizeof(i)) ;
}   
}
inventoryFile.close();   
}