Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ I have code for a inventory.dat file we are supposed to pull from. I re-read the directions and discovered we are not supposed to

C++ I have code for a inventory.dat file we are supposed to pull from. I re-read the directions and discovered we are not supposed to be using arrays. Any suggestions? Here is my code.

#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

{

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_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

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions

Question

Customers have to repeat information they have already provided.

Answered: 1 week ago