Question
Purpose: To get experience using structures and binary files. For this lab, please write a program that uses a structure to store the following inventory
Purpose: To get experience using structures and binary files.
For this lab, please write a program that uses a structure to store the following inventory data in a file:
1.Item Description ( Must be a char array)
2.Quantity on Hand (int)
3.Wholesale Cost (double)
You must use the starter program below (page 2) and you should not add anything to main. Just write the code for:
1. addRecord (10 points)
2. viewRecord (8 points)
3. changeRecord (10 points)
Input validation for Item Quantity and Cost: (4 X 3 = 12 points)
1. Must be numeric (4 points each)
2. Must not be negative (4 points each)
3.Must be greater than zero (4 points each)
There is no validation for the Item Description.
File specifications: (2 points)
You must submit the test file (with reasonable inventory names) that you created while testing your program.
The test file should have at least 5 entries and have an extension of .dat
The file path to the test file should be C:\temp
*** The file format MUST be binary***.
On Exit: (2 Points. NOTE: this requirement is not represented on the sample output below)
Print to the screen: Thank you for visiting the
where
STARTUP CODE:
//*** Your Name Here ***
#include
#include
#include
struct Inventory
{
char desc[30];
int qty;
double wholeSaleCost;
};
// Function prototypes
void addRecord(fstream &);
void viewRecord(fstream &);
void changeRecord(fstream &);
int main()
{
using namespace std;
fstream inventoryFile;
int choice;
cout << setprecision(2) << fixed;
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
{
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;
}
Hints:
1. Refer to textbook example progams 12-13 through 12-22.
2.The backslash is used to separate folders in the file path on Microsoft File Systems. e.g. C:\Temp\Dog.dat
File names in Unix use a forward slash e.g. C:/Temp/Dog.dat
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++ on MS Windows we can use two backslashes \\ to signal the compiler that the next character is a special character and that special character is the backslash.
Note: C++ recognizes the Unix/Linux version and automatically converts it to the appropriate operating system style. This means file names in Microsoft Windows can be written as C:\\Temp\\Dog.dat or C:/Temp/Dog.dat. (Use the windows version)
3.You should manually delete the inventory test file every time you run your program. If you dont 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.
4. 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 is to force the students to process the input line as an array of characters**
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started