Take the given code and modify it so when choice number 3 ispicked then it shows what the item currently is and then asks forthe new modified information…for example
Inventory Program Menu 1. ADD NEW Record 2. DISPLAY Record 3. MODIFY Record 4. EXIT Programplease enter selection (1 – 4) : 3Which record to MODIFY:Please choose one of the following… 1 to 2 : 2Description: TeaQuantity: 30Wholesale Price: $30.00Retail Price: $40.00Date: 03032018Enter MODIFY Data:Desciption: TEa PowderQuantity: 10Wholesale Price: $10Retail Price: $15Date Added: 03032017
The original question was…
Write a program that uses a structure to store the followinginventory information in a Binary file and access it using RandomAccess Method:
Item description
Quantity on hand
Wholesale cost
Retail cost
Date added to inventory
The program should have a menu that allows the user to performthe following tasks:
Add new records to the file
Display any record in the file
Change any record in the file
#include
#include
using namespace std;
typedef struct InventoryData
{
//declare required structure for inventory
char iDesc[50];
int quantity;
double wCost, rCost;
char date[10];
} InventoryD;
//function definition for openInventoryFile
void
openInventoryFile(fstream & inventory)
{
//Function to open file InventoryData.dat
inventory.open(“inventory.dat”, ios::out | ios::in |ios::binary);
if (inventory.fail())
{
//If file the failed to open
inventory.open(“inventory.dat”,
ios::out |ios::in | ios::binary | ios::trunc);
if (inventory.fail())
{
//If file failedto open
cout <<“Error opening file….”;
exit(0);
}
else
return;
}
else
return;
}
//function definition for addRecord
void
addRecord(fstream & inventory)
{
//Function to addRecord new record to file
InventoryD item;
inventory.clear(); //Toclear the state back to good
inventory.seekp(0, ios::end); //To movefile pointer to end to append
cin.ignore();
cout << “nEnter the NEW Record Data:”;
cout << “nDescription: “;
cin.getline(item.iDesc, 50);
cout << “Quantity: “;
cin >> item.quantity;
cout << “Wholesale Price: $”;
cin >> item.wCost;
cout << “Retail Price: $”;
cin >> item.rCost;
cin.ignore();
cout << “Date: “;
cin.getline(item.date, 10);
inventory.write((char *)&item,sizeof(item));
}
//function definition for printRecord
void
printRecord(fstream & inventory)
{
InventoryD item;
inventory.clear();
inventory.seekg(0, ios::beg);
int num = 0;
while (inventory.read((char *)&item,sizeof(item)))
num++;
//if the given file is empty
if (num == 0)
{
//File is empty
cout << “nNo record found.File empty”;
return;
}
//otherwise for not empty
else
{
cout << “nWhich Record toDISPLAY:”;
cout << “nPlease choose oneof the following… 1 to ” << num << ” : “;
cin >> num;
inventory.clear();
inventory.seekg(0, ios::beg);
int i = 0;
while (inventory.read((char*)&item, sizeof(item)))
{
//Read everyrecord and check if i == num
i++;
//if equal, thendisplay the given record
if (i ==num)
{
cout << “nDescription: ” <<item.iDesc;
cout << “nQuantity: ” <<item.quantity;
cout << “nWholesale Price: $” <<item.wCost;
cout << “nRetail Price: $” <<item.rCost;
cout << “nDate Added: ” <<item.date;
return;
}
}
}
}
//function definition for modify the data record
void
modifyRecord()
{
InventoryD item;
fstream inventory;
openInventoryFile(inventory);
inventory.clear();
inventory.seekg(0, ios::beg);
fstream o(“temp.dat”, ios::binary | ios::out);
int num = 0;
while (inventory.read((char *)&item,sizeof(item)))
num++;
if (num == 0)
{
cout << “nNo record found.File empty”;
return;
}
else
{
cout << “nWhich Record toMODIFY:”;
cout << “nPlease choose oneof the following… 1 to ” << num << ” : “;
cin >> num;
inventory.clear();
inventory.seekg(0, ios::beg);
int i = 0;
while (inventory.read((char*)&item, sizeof(item)))
{
i++;
if (i ==num)
{
cin.ignore();
cout << “Enter the NEW data to MODIFYrecord:”;
cout << “Desciption: “;
cin.getline(item.iDesc, 50);
cout << “Quantity: “;
cin >> item.quantity;
cout << “Wholesale Price: $”;
cin >> item.wCost;
cout << “Retail Price: $”;
cin >> item.rCost;
cin.ignore();
cout << “Date: “;
cin.getline(item.date, 10);
}
o.write((char*)&item, sizeof(item));
}
}
inventory.close();
o.close();
remove(“inventory.dat”);
rename(“temp.dat”, “inventory.dat”);
}
//main function
int
main()
{
//for file
fstream inventory;
//call openInventoryFile function
openInventoryFile(inventory);
int choice;
do
{
//display the menu
cout << “Inventory ProgramMenu”;
cout << “n 1. ADD NEWRecord”;
cout << “n 2. DISPLAYRecord”;
cout << “n 3. MODIFYRecord”;
cout << “n 4. EXITProgram”;
cout << “nplease enterselection (1 – 4) : “;
cin >> choice;
switch (choice)
{
case 1:
addRecord(inventory);
break;
case 2:
printRecord(inventory);
break;
case 3:
inventory.close();
modifyRecord();
openInventoryFile(inventory);
break;
case 4:
break;
default:
cout <<“nPlease enter a valid choice (1-4) “;
}
} while (choice != 4);
cout << “nThank You!n”;
inventory.close();
return 0;
}
Expert Answer
Answer to Take the given code and modify it so when choice number 3 is picked then it shows what the item currently is and then as…