int main() do {// Display the menu. cout << \"\ 1. Add a new record\ \"; cout <<
ID: 3799905 • Letter: I
Question
int main()
do
{// Display the menu.
cout << " 1. Add a new record ";
cout << "2. View an existing record by record number "; cout << "3. Change an existing record ";
cout << "4. Exit ";
do
{
{// Choice 1 is to add a record.
case 1:addRecord(inventoryFile); break;
case 2:viewRecord(inventoryFile); break;
case 3:
return 0; }
Hints:
The backslash is used to separate levels in the Microsoft File System e.g. C:TempDog.dat
File names in Unix use a forward slash e.g. C:/Temp/Dog.dat
Those using Unix must convert the files names before handing in the file
Because C++ uses the backslash as a marker meaning that the next character is a special character (like means CR + LF – two characters), we must use special notation to get a backslash.
Note that in UNIX the means only one character entry CR the operating system treats as a CR + LF.
In C++ we use two backslashes \ to signal the compiler that the next character is a special character and that special character is the backslash.
So file names in Microsoft look like this C:\Temp\Dog.dat
– 5 point penalty if not done.
You might want to manually delete the inventory file every time you run your program.
If you don’t delete the inventory file and run your program again, you may start working with a file that is already in existence as opposed to creating a new file.
When coding the input validation, do it such a way that it can be used by other programs with minimum effort.
Note – students have asked: Why was the string class not included? This prevents the use of getline() and substring methods that could have made the code more organized and efficient.
The purpose was to force the students to process the input line as an array of characters.
***********Please DO NOT answer if you don't know********************
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;
struct InventoryItem // inventory structure catagories
{
char desc[30];
int qty;
double wholeSaleCost;
};
void addRecord(fstream &); // function prototype to add a record
void viewRecord(fstream &); // function prototype to view a record
void changeRecord(fstream &); // function prototype to change a record
int main()
{
fstream inventoryFile ("Inventory.dat", ios::in | ios::out | ios::binary);
int choice;
cout << fixed << showpoint << setprecision(2);
inventoryFile.open("Inventory.dat", ios::out | ios::binary);
cout<<"Inventory Managment"<<endl;
do
{
cout << " 1. Add a new record ";
cout << "2. View an existing record by record number ";
cout << "3. Change an existing record ";
cout << "4. Exit ";
do
{
cout << "Enter your choice (1-4): ";
cin >> choice;
}while (choice < 1 || choice > 4);
switch (choice)
{
case 1:addRecord(inventoryFile); break;
case 2:viewRecord(inventoryFile); break;
case 3: changeRecord(inventoryFile); break;
}
}while (choice != 4);
inventoryFile.close();
return 0;
}
void addRecord(fstream &file) // add new information to inventoryFile
{
cout << "Enter inventory data"<<endl;
fstream inventoryFile("Inventory.dat", ios::out | ios::binary);
InventoryItem record;
cout << "Description : ";
cin.getline(record.desc, 51);
cin >> record.desc;
cout << "Quantity : ";
cin >> record.qty;
cout << "Wholesale cost : ";
cin >> record.wholeSaleCost;
inventoryFile.write(reinterpret_cast<char *>(&record),sizeof(record));
cout << "Record added to the file."<<endl;
file.close();
}
void viewRecord(fstream &file)
{
fstream inventoryFile("Inventory.dat", ios::out | ios::binary);
InventoryItem record;
long recNum;
cout << "Enter the record number :";
cin >> recNum;
inventoryFile.seekg(recNum * sizeof(record), ios::beg);
inventoryFile.read(reinterpret_cast<char *>(&record),sizeof(record));
cout << "Description : " << record.desc << endl;
cout << "Quantity : " << record.qty << endl;
cout << "Wholesale cost : " << record.wholeSaleCost << endl;
if(file.fail())
file.clear();
file.close();
}
void changeRecord(fstream &file)
{
fstream inventoryFile ("InventoryFile.dat", ios::out | ios::binary);
InventoryItem record;
long recNum;
cout << "Enter the record number to edit : ";
cin >> recNum;
inventoryFile.seekg(recNum * sizeof(record), ios::beg);
inventoryFile.read(reinterpret_cast<char *>(&record),sizeof(record));
cout << "Description : " << record.desc << endl;
cout << "Quantity : " << record.qty << endl;
cout << "Wholesale cost : " << record.wholeSaleCost << endl;
inventoryFile.seekp(recNum * sizeof(record), ios::beg);
inventoryFile.write(reinterpret_cast<char *>(&record),sizeof(record));
inventoryFile.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.