Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Your inventory structure is incorrect. quantity should be an integar cost and wholesale should be double. i need to correct and make it work,

C++

"Your inventory structure is incorrect. quantity should be an integar cost and wholesale should be double."

i need to correct and make it work, works but my professor wants me to change char to int and double.

#include #include #include

using namespace std;

const char ItemNameSIZE = 25; const char dateSIZE = 11; const char costSIZE = 11; const char numItems = 11;

struct Inventory

{ char description[ItemNameSIZE]; char quantity[numItems]; char wholesaleCost[costSIZE]; char retailCost[costSIZE]; char date[dateSIZE];

};

void menu(); void welcome(); void CreateABinaryFile(fstream&); void WriteRecordsToFile(fstream&); void RetrieveARecordfromFile(fstream&); void ReadABinaryFile(fstream&); void UpdateRecord(fstream&);

int main() {

int choice; welcome();

fstream file; menu(); int RecSize = sizeof(Inventory); cout << "The size of one item record is: " << RecSize << " "; cout << "Enter a choice: "; cin >> choice; while (choice >= 1 && choice <= 5) { switch (choice) { case 1: CreateABinaryFile(file);

break;

case 2: WriteRecordsToFile(file); cout << "Done writing info. "; break;

case 3: ReadABinaryFile(file); break;

case 4: RetrieveARecordfromFile(file); break;

case 5: UpdateRecord(file); break;

default: cout << "Bye... ";

}

cout << "Enter a choice: "; cin >> choice;

}

system("cls"); menu(); cin.ignore(); cin.get(); return 0; }

void welcome() { string name; cout << "Welcome to the CBC Inventory app ";

cout << "Please enter your name "; getline(cin, name); cout << " "; cout << "Welcome " << name << ", ";

} void menu() {

cout << "1\tCreate a binary file. " << " "; cout << "2\tWrite information to file. " << " "; cout << "3\tRead information to file. " << " "; cout << "4\tRetrive a record from file." << " "; cout << "5\tUpdate a record. " << " "; cout << "6\tExit... " << " ";

}

void CreateABinaryFile(fstream& binfile) { binfile.open("inventory.dat", ios::out | ios::binary); if (!binfile) { cout << "Error opening file."; cin.ignore(); cin.get(); exit(1); }

binfile.close();

}

void WriteRecordsToFile(fstream& binfile) { binfile.open("inventory.dat", ios::out | ios::binary); if (!binfile) { cout << "Error opening file."; cin.ignore(); cin.get(); exit(1); } else { Inventory record; char answer = 'Y';

do { cin.getline(record.description, ItemNameSIZE); cout << "Enter a item description: "; cin.getline(record.description, ItemNameSIZE); cout << "Enter the quantity on hand: "; cin.getline(record.quantity, numItems); cout << "Enter the wholesale cost: "; cin.getline(record.wholesaleCost, costSIZE); cout << "Enter the retail cost: "; cin.getline(record.retailCost, costSIZE); cout << "Enter todays date (00/00/0000) : "; cin.getline(record.date, dateSIZE);

binfile.write(reinterpret_cast(&record), sizeof(record));

cout << "Do you want to enter another record? "; cin >> answer; cin.ignore();

} while (answer == 'y' || answer == 'Y'); }

binfile.close(); }

void ReadABinaryFile(fstream& binfile) { binfile.open("inventory.dat", ios::in | ios::binary); if (!binfile) { cout << "Error opening file."; cin.ignore(); cin.get(); exit(1); } else { Inventory record; binfile.read(reinterpret_cast(&record), sizeof(record)); while (!binfile.eof()) { cout << "ITEM QUANTITY WSCOST RETCOST DATE "; cout << record.description << "\t " << record.quantity << "\t\t " << record.wholesaleCost << "\t\t " << record.retailCost << "\t\t " << record.date << endl; cout << " Strike any key to see the next record. "; cin.get(); binfile.read(reinterpret_cast(&record), sizeof(record));

}

} binfile.close(); }

void RetrieveARecordfromFile(fstream& binfile) { binfile.clear(); binfile.open("Inventory.dat", ios::in | ios::binary); if (!binfile) { cout << "Error opening file."; cin.ignore(); cin.get(); exit(1); } else { Inventory record; int recnum = 0; cout << "What record do you want to retrieve? " << endl; cin >> recnum; binfile.seekg((recnum - 1) * sizeof(record), ios::beg); binfile.read(reinterpret_cast(&record), sizeof(record));

cout << "Inventory of record # " << recnum << " contains " << endl;

cout << "ITEM QUANTITY WSCOST RETCOST DATE "; cout << record.description << "\t " << record.quantity << "\t\t " << record.wholesaleCost << "\t\t " << record.retailCost << "\t\t " << record.date << endl; }

binfile.close(); }

void UpdateRecord(fstream& binfile) { //binfile.clear(); binfile.open("Inventory.dat", ios::out | ios::in | ios::binary); if (!binfile) { cout << "Error opening file."; cin.ignore(); cin.get(); exit(1); } else { Inventory record; int recnum = 0; cout << "What record do you want to update? " << endl; cin >> recnum; binfile.seekg((recnum - 1) * sizeof(record), ios::beg); binfile.read(reinterpret_cast(&record), sizeof(record)); cout << "This record was found and is going to be update: "; cout << "Inventory record number: " << recnum << "contains "; cout << "ITEM QUANTITY WSCOST RETCOST DATE "; cout << record.description << "\t " << record.quantity << "\t\t " << record.wholesaleCost << "\t\t " << record.retailCost << "\t\t " << record.date << endl; cin.ignore(); cout << "Enter a item description: "; cin.getline(record.description, ItemNameSIZE); cout << "Enter the quantity on hand: "; cin.getline(record.quantity, numItems); cout << "Enter the wholesale cost: "; cin.getline(record.wholesaleCost, costSIZE); cout << "Enter the retail cost: "; cin.getline(record.retailCost, costSIZE); cout << "Enter todays date (00/00/0000) : "; cin.getline(record.date, dateSIZE);

binfile.seekp((recnum - 1) * sizeof(record), ios::beg); binfile.write(reinterpret_cast(&record), sizeof(record));

} binfile.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

C++ Database Development

Authors: Al Stevens

1st Edition

1558283579, 978-1558283572

Students also viewed these Databases questions

Question

Explain the importance of Human Resource Management

Answered: 1 week ago

Question

Discuss the scope of Human Resource Management

Answered: 1 week ago

Question

Discuss the different types of leadership

Answered: 1 week ago

Question

Write a note on Organisation manuals

Answered: 1 week ago

Question

Define Scientific Management

Answered: 1 week ago