Question: C++ Assignment I have to write a menu-driven program that updates the binary inventory file, inventory.dat. The information reflected in the Struct is correct. It

C++ Assignment I have to write a menu-driven program that updates the binary inventory file, inventory.dat. The information reflected in the Struct is correct.

It must implement the following functions:

a) int showMenu (displays the menu, inputes the selection and validates the user's selection)

b) void printFile (fstream&); (clear the eof flag, seek the first record, use an eof loop to read the file into a product record and display the fileds to the screen, use a counter in the loop to count and display the record number)

c) void displayRecord (fstream&); (input user's record number selection, clear the eof flag, seek the first record, seek the indicated record number, read the file and display product info to the screen)

d) void modifyRecord(fstream&); (input user's record number selection, input new values for selected record number, clear the eof flag, seek the first record, seek the indicated record number and write new values to file to replace existing data.)

Main program must use a while loop with embedded switch statement to call appropriate functions. All operations must access the file. No array can be used. Below is the code I have so far.

#include

#include

#include

#include

using namespace std;

const int DESC_SIZE = 40;

struct Product

{

long prodNum;

char prodName[DESC_SIZE];

double price;

int qty;

};

//function prototypes

int showMenu();

void printFile(fstream&);

void displayRecord(fstream&);

void modifyRecord(fstream&);

//main function

int main()

{

long choice;

//declare a fstream file object

fstream inventory;

do

{

cjpoce =, emi();

switch choice

{

case 1: //Display product inventory file

printFile(inventory);

break;

case 2: //Display a particular product

displayRecord(inventory);

break;

case 3: //Modify a product

modifyRecord(inventory);

break;

default: //Invalid selection

cout << "Invalid selection." << endl;

}

while (choice != 4);

cout << "Thank you. Good bye." << endl;

inventory.close();

system("pause");

return 0;

}

// function that prompts user to enter choice

int menu()

{

cout << "1. Display product inventory file ";

cout << "2. Display a particular product ";

cout << "3. Modify a product ";

cout << "4. Exit the program. ";

cout << endl;

cout << "Enter Menu Choice: ";

cin >> choice;

while (choice < 1 || choice > 4)

{

cout << "Invalid selection. Please enter your choice (1 - 4): ";

cin >> choice;

}

cout << endl;

return choice;

//function printFile that opens input file in read mode and

//display all items.

void printFile(fstream &file)

{

file.open("Inventory.dat", ios::out | : ios::binary | ios::app);

cout

}

//function displayRecord that opens Inventory file in read mode and

//will prompt user to enter record number and display record.

void displayRecord

{

file.open("Inventory.dat", ios::in | ios::binary);

InventoryItem record;

cin.clear();

cout << "Enter record number to be displayed: ";

cin >> record.partNo;

file.seekp((record.partNo - 1) * sizeof(record), ios::beg);

file.read((char *)(&record), size of(InventoryItem));

cout << "Product Number: " << record.Num << endl;

cout << "Product Name: " << record.Name << endl;

cout << "Price: " << record.Price << endl;

cout << "Quantity: " << recordQuan << endl;

cout << endl;

file.close();

}

//function modifyRecord that will open file in read for reading file

//record and close and open the file and write the user entered record

//items to file to same location

void modifyRecord(fstream &file)

{

file.open("Inventory.dat", ios::in | ios::binary);

InventoryItem record;

fflush(stdin);

cout << "Enter record number to be modified: ";

cin >> record.partNo

//move to the record to read item

file.seekg((record.partNo - 1) * sizeof(InventoryItem), ios::beg);

file.read((char *)(&record), sizeof(InventoryItem));

cout << "Product Number: " << record.Num << endl;

cout << "Product Name: " << record.Name << endl;

cout << "Price: " << record.Price << endl;

cout << "Quantity: " << record.Quan << endl;

cout << endl;

//close file

file.close();

cout << "Enter the new data." << endl;

cin.ingnore();

cin.getline(record.Num);

do

{

cout << "Product Number: ";

cin >> record.Num

if (record.Num < 0)

cout << "Invalid Product Number" << endl;

} while (record.Num < 0);

do

cout << "Product Name: "

cin >> record.Name;

cout << "Price: "

cin >> record.Price;

cout >> "Quantity: "

cin >> record.Quan;

//open file in write mode

file.open("Inventory.dat", ios::out | ios::binary);

// move back to the beginning of the record

file.seekg((record.partNo - 1) * sizeof(InventoryItem), ios::beg);

//write new record on old record

file.write((char*)(&record), sizeof(InventoryItem));

file.close();

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!