Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ assignment - I am stuck on code that will pull data from an inventory.dat file and display all items. Below is my code. I

C++ assignment - I am stuck on code that will pull data from an inventory.dat file and display all items.

Below is my code. I have indicated with a comment in the code where I'm having a problem at.

#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. This is where I am stuck.

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

In Exercises find the indefinite integral. S ex tan(ex) dx

Answered: 1 week ago