Using Microsoft Visual 2015 or 2017 C++, write a menu-driven program that update
ID: 3910456 • Letter: U
Question
Using Microsoft Visual 2015 or 2017 C++, write a menu-driven program that updates the binary inventory file, inventory.dat. The file is composed of records based on the following structure: const int DESC_SIZE = 40; struct Product { long prodNum; char prodName[DESC_SIZE]; double price; int qty; }; Requirements 1. Use a menu-driven program that implements the following commands: a. Display product inventory file b. Display a particular product c. Modify a product d. Exit the program 2. Implement the following functions: a. int showMenu(); Displays the menu Inputs the user’s selection Validates the user’s selection b. void printFile (fstream&); Clear the eof flag Seek the first record (record 0) Use an eof loop to read the file into a Product record and display the fields to the screen Use a counter in your loop to count and display the record number c. void displayRecord( fstream&); Input the user’s record number selection Clear the eof flag Seek the first record (record 0) Seek the indicated record number Read the file and displays the product record to the screen d. void modifyRecord (fstream&); Input the user’s record number selection Input new values for the selected record number Clear the eof flag Seek the first record (record 0) Seek the indicated record number Write the new values to file to replace the existing data 3. Main program uses a while loop with embedded switch statement to call appropriate functions based on menu choice. 4. All operations must access the file – do NOT use an array. 5. Output must be labelled and easy to read. 6. Program must be documented with the following: Name, Date, Program Name, and Description
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int DESC_SIZE = 40;
struct Product {
long prodNum;
char prodName[DESC_SIZE];
double price; int qty;
};
int showMenu()
{ int choice=0;
cout<<endl<<"1. Display product inventory file"<<endl;
cout<<"2. Display a particular product"<<endl;
cout<<"3. Modify a product"<<endl;
cout<<"4. Exit the program"<<endl;
do
{
cout<<"Enter your choice between 1 to 4:"<<endl;
cin>>choice;
}while(choice<1 || choice>4);
return(choice);
}
void printFile (fstream &infile)
{
struct Product p;
int recNo=1;
infile.clear();
infile.seekg(0, ios::beg) ;
cout<<endl<<setw(15)<<left<<"Record-No"<<setw(15)<<left<<"Product-No"<<setw(15)<<left<<"Product-Name"<<setw(15)<<left<<"Price"<<setw(15)<<left<<"Quantity"<<endl;
while( infile.read( (char*)&p, sizeof(p)) )
{
cout<<endl<<setw(15)<<left<<recNo<<setw(15)<<left<<p.prodNum<<setw(15)<<left<<p.prodName<<setw(15)<<left<<p.price<<setw(15)<<left<<p.qty<<endl;
recNo++;
}
}
void displayRecord( fstream &infile)
{
struct Product p;
int recNo;
infile.clear();
infile.seekg(0, ios::beg) ;
cout<<"Enter the record number to be displayed:";
cin>>recNo;
infile.seekg((recNo-1)*sizeof(p), ios::beg) ;
infile.read( (char*)&p, sizeof(p));
cout<<endl<<setw(15)<<left<<"Record-No"<<setw(15)<<left<<"Product-No"<<setw(15)<<left<<"Product-Name"<<setw(15)<<left<<"Price"<<setw(15)<<left<<"Quantity"<<endl;
cout<<endl<<setw(15)<<left<<recNo<<setw(15)<<left<<p.prodNum<<setw(15)<<left<<p.prodName<<setw(15)<<left<<p.price<<setw(15)<<left<<p.qty<<endl;
}
void modifyRecord (fstream &infile)
{
struct Product p;
int recNo;
infile.clear();
infile.seekg(0, ios::beg) ;
cout<<"Enter the record number to be modifid:";
cin>>recNo;
infile.seekp((recNo-1)*sizeof(p), ios::beg) ;
cout<<"Enter the product number:";
cin>>p.prodNum;
cout<<"Enter the product name:";
cin>>p.prodName;
cout<<"Enter the product price:";
cin>>p.price;
cout<<"Enter the product quantity:";
cin>>p.qty;
infile.write( (char*)&p, sizeof(p));
}
int main()
{
int choice;
fstream infile;
infile.open("inventory.dat", ios::in | ios::out | ios::binary);
do
{
choice = showMenu();
switch(choice)
{
case 1: printFile (infile);
break;
case 2: displayRecord(infile);
break;
case 3: modifyRecord(infile);
}
}while(choice!=4);
infile.close();
}
For executing the above code first we need to create "inventory.dat" binary file by executing following program:
#include <iostream>
#include <fstream>
using namespace std;
const int DESC_SIZE = 40;
struct Product {
long prodNum;
char prodName[DESC_SIZE];
double price; int qty;
};
void createfile()
{
int i=1;
fstream outfile;
struct Product p;
outfile.open("inventory.dat", ios::out| ios::binary);
while(i<=5)
{
cout<<"Enter the product number:";
cin>>p.prodNum;
cout<<"Enter the product name:";
cin>>p.prodName;
cout<<"Enter the product price:";
cin>>p.price;
cout<<"Enter the product quantity:";
cin>>p.qty;
outfile.write( (char*)&p, sizeof(p));
i++;
}
outfile.close();
}
int main()
{
createfile();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.