In: Computer Science
Write a program that uses a structure to store the following inventory information in a Binary file and access it using Random Access 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 perform the following tasks: Add new records to the file Display any record in the file Change any record in the file For exact menu option text please see the test cases below
Solution
Code
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cctype>
using namespace std;
struct Inventory
{
string description,Date;
int quantity;
double wholesalecost, RetailCost;
};
void addnewrecord(Inventory &, fstream &);
void displayanyrecord(Inventory &, fstream &);
void changeanyrecord(Inventory &, fstream &);
long byteNum(int);
void showerror();
bool checkdate(string);
int main ()
{
Inventory record;
int Input;
fstream File("inventory.dat", ios::out | ios::in |
ios::binary);
if (!File)
{
cout << "Error opening
file.\n";
return 0;
}
cout << " Inventory \n"
<< "Choose othe
option:\n";
cout << " 1. Adding new records to the
file.\n";
cout << " 2. Displaying any record in the
file.\n";
cout << " 3. Changing any record in the
file.\n";
cin >> Input;
switch (Input)
{
case 1 : addnewrecord(record,
File);
break;
case 2 : displayanyrecord(record,
File);
break;
case 3 : changeanyrecord(record,
File);
}
File.close();
return 0;
}
void addnewrecord(Inventory &record, fstream &File)
{
File.seekp(0L, ios::end);
cout << "Enter the following inventory
information:\n";
cout << "Item description: ";
cin.ignore();
getline(cin, record.description);
do
{
cout << "Date in the format
MM/DD/YYYY: ";
cin.ignore();
getline(cin, record.Date);
if (checkdate(record.Date) ==
0)
{
cout <<
checkdate(record.Date) << endl;
cout <<
"Error! Invalid date format.\n";
}
} while (checkdate(record.Date) == 0);
do
{
cout << "Quantity :";
cin >> record.quantity;
if (record.quantity < 0)
showerror();
} while (record.quantity < 0);
do
{
cout << "Wholesale cost:
";
cin >>
record.wholesalecost;
if (record.quantity < 0)
showerror();
} while (record.wholesalecost < 0);
do
{
cout << "Retail cost:
";
cin >>
record.RetailCost;
if (record.RetailCost < 0)
showerror();
} while (record.RetailCost < 0);
File.write(reinterpret_cast<char
*>(&record), sizeof(record));
}
void displayanyrecord(Inventory &record, fstream
&File)
{
int recNum;
cout << "Enter the record number: ";
cin >> recNum;
if (recNum < 1)
recNum = 1;
recNum--;
File.seekg(byteNum(recNum), ios::beg);
File.read(reinterpret_cast<char *>(&record),
sizeof(record));
cout << "Record number: " << (recNum + 1)
<< endl;
cout << "Item description: ";
cout << record.description << endl;
cout << "Date : ";
cout << record.Date << endl;
cout << "Quantity :";
cout << fixed << showpoint <<
setprecision(2);
cout << record.quantity << endl;
cout << "Wholesale cost: ";
cout << record.wholesalecost <<
endl;
cout << "Retail cost: ";
cout << record.RetailCost << endl;
}
void changeanyrecord(Inventory &record, fstream
&File)
{
int recNum;
cout << "Enter the record number: ";
cin >> recNum;
if (recNum < 1)
recNum = 1;
recNum--;
File.seekg(byteNum(recNum), ios::beg);
File.read(reinterpret_cast<char *>(&record),
sizeof(record));
cout << "Enter the following inventory
information:\n";
cout << "Item description: ";
cin.ignore();
getline(cin, record.description);
do
{
cout << "Date in the format
MM/DD/YYYY: ";
cin.ignore();
getline(cin, record.Date);
if (!checkdate(record.Date))
{
cout <<
checkdate(record.Date) << endl;
cout <<
"Error! Invalid date format.\n";
}
} while (!checkdate(record.Date));
do
{
cout << "Quantity :";
cin >> record.quantity;
if (record.quantity < 0)
showerror();
} while (record.quantity < 0);
do
{
cout << "Wholesale cost:
";
cin >>
record.wholesalecost;
if (record.quantity < 0)
showerror();
} while (record.wholesalecost < 0);
do
{
cout << "Retail cost:
";
cin >>
record.RetailCost;
if (record.RetailCost < 0)
showerror();
} while (record.RetailCost < 0);
File.seekp(byteNum(recNum), ios::beg);
File.write(reinterpret_cast<char
*>(&record), sizeof(record));
}
long byteNum(int recNum)
{
return sizeof(Inventory) * recNum;
}
void showerror()
{
cout << "Error the number should be greater than
0.\n";
}
bool checkdate(string date)
{
if ( date.length() == 9)
date = "0" + date;
if (date.length() != 10)
return false;
for (int i = 0; i < date.length(); i++)
{
if (i == 2 || i == 5)
{
if (date[i] !=
'/')
{
return false;
}
continue;
}
if (!isdigit(date[i]))
return
false;
}
return true;
}
Screenshot







---
all the best