Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ assignment. I have a program where I forgot to create the inventory.dat file I need for the program to pull from. It must allow

C++ assignment. I have a program where I forgot to create the inventory.dat file I need for the program to pull from. It must allow me to have four items stored Product Number, Product Name, Price and Quantity. Most of my code I think is correct but I know I am missing the following. My code is below.

For voidprintFile - I am missing clear the eof flag and seek the first record. I need an eof loop to read the file into the product record and display the record number. Use a counter in your loop to count and display the record number.

For void displayRecord - I am missing the clear the eof flag, seek the first record, seek the indicated record numbver, read the file and display to the screen.

for void modifyRecord - clear the eof flag, seek the first record, seek the indicated record number and write the new values to file to replace existing data.

no arrays can be used.

#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

{

choice = showMenu();

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()

{

int choice;

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);

for (int i - 0; i < 4; i++)

}

file >> array[i].prodNum;

file >> array[i].prodName;

file >> array[i].price;

file >> array[i].qty;

}

cout << " The content from the file is:" << endl;

cout << " ProductNo\tProductName\tProduct Price\tQuantity " << endl;

for (int i = 0; i < 4; i++)

{

cout << array[i].prodNum << "\t\t:" << array[i].prodName << "\t\t" << array[i].price << "\t\t" << array[i].qty << " "

<< endl;

}

cout << endl;

}

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

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

void displayRecord(fstream, &file)

{

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

struct Product record;

cin.clear();

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

cin >> record.prodName;

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

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

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

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

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

cout << "Quantity: " << record.qty << 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);

Struct Product record;

fflush(stdin);

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

cin >> record.partNo

//move to the record to read item

file.seekg((record.prodNum - 1) * sizeof(Product), ios::beg);

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

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

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

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

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

cout << endl;

//close file

file.close();

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

cin.ignore();

do

{

cout << "Product Number: ";

cin >> record.prodNum;

if (record.prodNum < 0)

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

}

} while (record.prodNum < 0);

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.prodNum - 1) * sizeof(Product), ios::beg);

//write new record on old record

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

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

Step: 3

blur-text-image

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

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions