Need help with output error as defined

Inventory Program

Write a program that uses a structure to store the following inventory information in a Binary file and access it using Random Access Method:

Item description
Quantity on hand
Wholesale cost
Retail cost
Date added to inventory

The program should have a menu that allows the user to perform the following tasks:

Add new records to the file
Display any record in the file
Change any record in the file

For exact menu option text please see the test cases below:
Need help with output error as defined in test case 2

Test Case2:
Inventory Program Menu
1. ADD NEW Record
2. DISPLAY Record
3. MODIFY Record
4. EXIT Program
please enter selection (1 - 4) : 5
Please enter a valid choice (1 - 4): 4
Thank you!


My Answer
// This program write a structure to store the following
// inventory information in a Binary file and access it using Random Access Method:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

typedef struct InventoryData
{
//declare required structure for inventory
char iDesc[50];
int quantity;
float wCost, rCost;
char date[10];
}
InventoryD;
//function definition for openInventoryFile
void openInventoryFile(fstream& inventory)
{
//Function to open file InventoryData.dat
inventory.open("inventory.dat", ios::out | ios::in | ios::binary);
if (inventory.fail())
{
//If file the failed to open
inventory.open("inventory.dat", ios::out | ios::in | ios::binary | ios::trunc);
if (inventory.fail())
{
//If file failed to open
cout << "Error opening file....";
exit(0);
}
else
return;
}
else
return;
}
//function definition for addRecord
void addRecord(fstream& inventory)
{
//Function to addRecord new record to file
InventoryD item;
inventory.clear(); //To clear the state back to good
inventory.seekp(0, ios::end); //To move file pointer to end to append
cin.ignore();
cout << "\n Enter the NEW Record Data:";
cout << "\nDesciption: ";
cin.getline(item.iDesc, 50);
cout << "Quantity: ";
cin >> item.quantity;
cout << "Wholesale Price: $";
cin >> item.wCost;
cout << "Retail Price: $";
cin >> item.rCost;
cin.ignore();
cout << "Date Added: ";
cin.getline(item.date, 10);
inventory.write((char*)&item, sizeof(item));
}
//function definition for printRecord
void printRecord(fstream& inventory)
{
InventoryD item;
inventory.clear();
inventory.seekg(0, ios::beg);
int num = 0;
while (inventory.read((char*)&item, sizeof(item)))
num++;
//if the given file is empty
if (num == 0)
{
//File is empty
cout << "\nNo record found. File empty";
return;
}
//otherwise for not empty
else
{
cout << "\n Which Record to DISPLAY:";
cout << "\nPlease choose one of the following... 1 to " << num << " : ";
cin >> num;
inventory.clear();
inventory.seekg(0, ios::beg);
int i = 0;
while (inventory.read((char*)&item, sizeof(item)))
{
//Read every record and check if i == num
i++;
//if equal, then display the given record
if (i == num)
{
cout << "\nDescription: " << item.iDesc;
cout << "\nQuantity: " << item.quantity;
cout << "\nWholesale Price: $" << fixed << setprecision(2) << item.wCost;
cout << "\nRetail Price: $" << fixed << setprecision(2) << item.rCost;
cout << "\nDate: " << item.date;
return;
}
}
}
}
//function definition for modify the data record
void modifyRecord()
{
InventoryD item;
fstream inventory;
openInventoryFile(inventory);
inventory.clear();
inventory.seekg(0, ios::beg);
fstream o("temp.dat", ios::binary | ios::out);
int num = 0;
while (inventory.read((char*)&item, sizeof(item)))
num++;
if (num == 0)
{
cout << "\nNo record found. File empty";
return;
}
else
{
cout << "\nWhich record to MODIFY:";
cout << "\nPlease choose one of the following... 1 to " << num << " : ";
cin >> num;
inventory.clear();
inventory.seekg(0, ios::beg);
int i = 0;
while (inventory.read((char*)&item, sizeof(item)))
{
i++;
if (i == num)
{
cout << "\nDescription: " << item.iDesc;
cout << "\nQuantity: " << item.quantity;
cout << "\nWholesale Price: $" << item.wCost;
cout << "\nRetail Price: $" << item.rCost;
cout << "\nDate: " << item.date;
cin.ignore();
cout << "\nEnter MODIFY Data:";
cout << "\nDesciption: ";
cin.getline(item.iDesc, 50);
cout << "Quantity: ";
cin >> item.quantity;
cout << std::setprecision(4) << "\nWholesale Price: $";
cin >> item.wCost;
cout << std::setprecision(4) << "\nRetail Price: $";
cin >> item.rCost;
cin.ignore();
cout << "Date Added: ";
cin.getline(item.date, 10);
}
o.write((char*)&item, sizeof(item));
}
}
inventory.close();
o.close();
remove("inventory.dat");
rename("temp.dat", "inventory.dat");
}
//main function
int main()
{
//for file
fstream inventory;
//call openInventoryFile function
openInventoryFile(inventory);
int choice;
do {
//display the menu
cout << "\nInventory Program Menu\n";
cout << " 1. ADD NEW Record\n";
cout << " 2. DISPLAY Record\n";
cout << " 3. MODIFY Record\n";
cout << " 4. EXIT Program";
cout << "\nplease enter selection (1 - 4) :";
cin >> choice;
switch (choice)
{
case 1:
addRecord(inventory);
break;
case 2:
printRecord(inventory);
break;
case 3:
inventory.close();
modifyRecord();
openInventoryFile(inventory);
break;
case 4:
break;
default:
cout << " Please enter a valid choice (1 - 4):" << endl;
}
}
while (choice != 4);
cout << "Thank you! ";
inventory.close();
return 0;
}
Last edited on
Topic archived. No new replies allowed.