Inventory Program Inventory Program Write a program that uses a structure to sto
ID: 3931013 • Letter: I
Question
Inventory Program
Inventory Program Write a program that uses a structure to store the following inventory data in a file: 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. Input Validation: The program should not accept quantities, or wholesale or retail costs, less than 0. The program should not accept dates that the programmer deter-mines are unreasonable.Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;
void viewRec(fstream &)
const int DESC_SIZE = 51;
const int DATE_SIZE = 11;
struct InventoryItem
{
char desc[DESC_SIZE];
int quantity;
double whlCost;
double rtlCost;
char date[DATE_SIZE];
};
int menu();
void addRec(fstream &);
void viewRec(fstream &);
void chgRec (fstream &);
int main()
{
long selection;
long recNum;
fstream inventory ("Inventory.dat", ios::in | ios::out | ios::binary);
InventoryItem record = {" ", 0, 0.0};
cout << fixed << showpoint << setprecision(2);
cout<<"Inventory Managment"<<endl;
for (int count = 0; count < 5; count++)
{
inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
}
inventory.close();
inventory.open("Inventory.dat", ios::out | ios::binary);
while (selection != 4)
{
switch (selection)
{
case 1:
{
addRec(inventory);
break;
}
case 2:
{
viewRec(inventory);
break;
}
case 3:
{
chgRec(inventory);
break;
}
default:
{
cout << "Invalid selection" << endl;
}
selection = menu();
}
}
cout<<"Hava a nice day."<<endl;
inventory.close();
system("pause");
return 0;
}
int menu()
{
int choice;
cout << "Please make a selection, 1 through 4." << endl;
cout << "1. Add a new record"<<endl;
cout << "2. View an exisitng record"<<endl;
cout << "3. Change an exisitng record"<<endl;
cout << "4. Exit"<<endl;
cout << endl;
cout <<"Enter your choice (1-4): ";
cin>>choice;
while(choice < 1 || choice > 4)
{
cout<<"Invaild selection!"<<endl;
cout<<"Please enter your choice (1-4) : ";
cin>>choice;
}
cout<<endl;
return choice;
}
void addRec(fstream &file)
{
cout<<"Enter the following inventory data:"<<endl;
fstream inventory("Inventory.dat", ios::out | ios::binary);
InventoryItem record;
cout<<"Description: ";
cin.ignore();
cin.getline(record.desc, 51);
cin>>record.desc;
cout<<"Quantity: ";
cin>>record.quantity;
cout<<"Wholesale cost: ";
cin>>record.whlCost;
cout<<"Retail price: ";
cin>>record.rtlCost;
cout<<"Date added to inventory (in 00/00/0000 format): ";
cin>>record.date;
inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
cout<<"Record added to file."<<endl;
file.close();
}
void dispRec(fstream &file)
{
fstream inventory ("Inventory.dat", ios::out | ios::binary);
InventoryItem record;
long recNum;
cout<<"Enter the record number of the item to view:";
cin>>recNum;
inventory.seekg(recNum * sizeof(record), ios::beg);
inventory.read(reinterpret_cast<char *>(&record),sizeof(record));
cout << "Description: " << record.desc << endl;
cout << "Quantity: " << record.quantity << endl;
cout << "Wholesale cost: " << record.whlCost << endl;
cout << "Retail price: " << record.rtlCost << endl;
cout << "Date (in 00/00/0000 format): " << record.date << endl;
cout << endl;
if(file.fail())
file.clear();
file.close();
}
void chgRec(fstream &file)
{
fstream inventory ("InventoryFile.dat", ios::out | ios::binary);
InventoryItem record;
long recNum;
cout << "Enter the record number of the item you want to edit: ";
cin >> recNum;
inventory.seekg(recNum * sizeof(record), ios::beg);
inventory.read(reinterpret_cast<char *>(&record),sizeof(record));
cout << "Description: " << record.desc << endl;
cout << "Quantity: " << record.quantity << endl;
cout << "Wholesale cost: " << record.whlCost << endl;
cout << "Retail price: " << record.rtlCost << endl;
cout << "Date (in 00/00/0000 format): " << record.date << endl;
cout << endl;
inventory.seekp(recNum * sizeof(record), ios::beg);
inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
inventory.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.