I need help with this C++ assignment For this lab, please write a program that u
ID: 3727126 • Letter: I
Question
I need help with this C++ assignment
For this lab, please write a program that uses a structure to store the following inventory data in a file: (2 points for each item 2 x 4 = 6 total points) 1. Item Description 2. Quantity on Hand 3. Wholesale Cost You must use the driver program below and you should not add anything to main. Just write the code for: 1. addRecord 2. viewRecord 3. changeRecord (10 points) (8 points) (10 points) Input validation for Item Quantity and Cost: (4 X 3-12 points) 1. Must be numeric 2. Must not be negative 3. Must be greater than zero (4 points each) (4 points each) (4 points each) There is no validation for Item Description File specifications: (2 points) You should provide a test file (with reasonable inventory names) that you have used to test your program The test file should have at least 5 entries and have an extension of“.dat" The file should be located in “C: emp" The file format MUST be binar On Exit: (2 Points. NOTE: this requirement is not represented on the sample output below) Print to the screen: "Thank you for visiting 's store" where is your FIRST name.Explanation / Answer
: Defines the entry point for the console application.
//
//#include <stdafx>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const string FILEPATH = "/temp/inventoryFile.dat";
struct Inventory
{
int itemNumber;
char desc[30];
int qty;
double wholeSaleCost;
};
// Function prototypes
void addRecord(fstream &datFile);
void viewRecord(fstream &datFile);
void changeRecord(fstream &datFile);
int main()
{
fstream inventoryFile;
int choice;
cout << setprecision(2) << fixed;
do
{
// Display the menu.
cout << " 1. Add a new record ";
cout << "2. View an existing record ";
cout << "3. Change an existing record ";
cout << "4. Exit ";
do
{
cout << "Enter your choice (1-4): ";
cin >> choice;
} while (choice < 1 || choice > 4);
// Process the selection.
switch (choice)
{
// Choice 1 is to add a record.
case 1:
addRecord(inventoryFile);
break;
// Choice 2 is to view a record.
case 2:
viewRecord(inventoryFile);
break;
// Choice 3 is to change a record.
case 3:
changeRecord(inventoryFile);
}
} while (choice != 4);
system ("pause");
return 0;
}
void addRecord(fstream &datFile)
{
Inventory record;
datFile.open("/temp/inventoryFile.dat", ios::out | ios::app | ios::binary);
if (datFile.fail())
{
cout << "ERROR: FILE DID NOT OPEN" << endl;
exit(EXIT_FAILURE);
}
cout << " Enter inventory info: ";
cout << "Item #: ";
cin >> record.itemNumber;
cin.ignore(1000, ' ');
while (record.itemNumber < 0)
{
cout << "Enter a valid item #: ";
cin >> record.itemNumber;
if (cin.fail() || record.itemNumber < 0)
{
cin.clear();
cin.ignore(1000, ' ');
}
}
cout << "Description: ";
cin.getline(record.desc, 30);
cout << "Quantity: ";
cin >> record.qty;
cin.ignore(1000, ' ');
while (record.qty < 0)
{
cout << "Enter valid quantity: ";
cin >> record.qty;
if (cin.fail() || record.qty <= 0)
{
cin.clear();
cin.ignore(1000, ' ');
}
}
cout << "Wholesale cost: ";
cin >> record.wholeSaleCost;
cin.ignore(1000, ' ');
while (record.wholeSaleCost < 0)
{
cout << "Enter valid cost: ";
cin >> record.wholeSaleCost;
if (cin.fail() || record.wholeSaleCost <= 0)
{
cin.clear();
cin.ignore(1000, ' ');
}
}
datFile.write(reinterpret_cast<char *>(&record), sizeof(record));
if (datFile.fail())
{
cout << "ERROR: COULD NOT WRITE TO FILE." << endl;
}
else
{
cout << "record written to file." << endl;
}
datFile.close();
}
void viewRecord(fstream &datFile)
{
Inventory record;
int recNum;
datFile.open("/temp/inventoryFile.dat", ios::in | ios::binary);
if (datFile.fail())
{
cout << "ERROR: COULD NOT OPEN FILE." << endl;
exit(EXIT_FAILURE);
}
cout << " Enter the record number of the item: ";
cin >> recNum;
datFile.seekg(recNum *sizeof(record), ios::beg);
if (datFile.fail())
{
cout << " ERROR: COULD NOT FIND FILE. ";
datFile.close();
exit(EXIT_FAILURE);
}
datFile.read(reinterpret_cast<char *>(&record), sizeof(record));
// datFile.close();
cout << "Item number: " << record.itemNumber << endl;
cout << "Description: " << record.desc << endl;
cout << "Quantity: " << record.qty << endl;
cout << "Wholesale cost: " << record.wholeSaleCost << endl;
datFile.close();
}
void changeRecord(fstream &datFile)
{
Inventory record;
int recNum;
datFile.open("/temp/inventoryFile.dat", ios::in | ios::out | ios::binary);
if (datFile.fail())
{
cout << "ERROR COULD NOT OPEN FILE. ";
exit(EXIT_FAILURE);
}
cout << "Enter the record number of the item: ";
cin >> recNum;
datFile.seekg(recNum *sizeof(record), ios::beg);
if (datFile.fail())
{
cout << "ERROR COULD NOT FIND FILE. ";
datFile.close();
exit(EXIT_FAILURE);
}
datFile.read(reinterpret_cast<char *>(&record), sizeof(record));
cout << "Current record contents: " << endl;
cout << "Item Number: " << record.itemNumber << endl;
cout << "Description: " << record.desc << endl;
cout << "Quantity: " << record.qty << endl;
cout << "Wholesale cost: " << record.wholeSaleCost << endl;
cout << " Enter the new data: " << endl;
cout << " Item #: ";
cin >> record.itemNumber;
cin.ignore(1000, ' ');
while (record.itemNumber < 0)
{
cin >> record.itemNumber;
if (cin.fail() || record.itemNumber <= 0)
{
cin.clear();
cin.ignore(1000, ' ');
cout << "Enter a valid item #: ";
}
}
cout << "Description: ";
cin.getline(record.desc, 30);
cout << "Quantity: ";
cin >> record.qty;
cin.ignore(1000, ' ');
while (record.qty < 0)
{
cout << "Enter valid quantity: ";
cin >> record.qty;
if (cin.fail() || record.qty <= 0)
{
cin.clear();
cin.ignore(1000, ' ');
}
}
cout << "Wholesale cost: ";
cin >> record.wholeSaleCost;
cin.ignore(1000, ' ');
while (record.wholeSaleCost < 0)
{
cout << "Enter valid cost: ";
cin >> record.wholeSaleCost;
if (cin.fail() || record.wholeSaleCost <= 0)
{
cin.clear();
cin.ignore(1000, ' ');
}
}
datFile.seekp(recNum *sizeof(record), ios::beg);
datFile.write(reinterpret_cast<char *>(&record), sizeof(record));
datFile.close();
}