Below is the beginning of a C++ program that uses fstream to write a structure to a binary file.
I have tried to use cin.ignore() to skip over newline chars that are causing cin reads to be skipped. It worked for several runs, followed by persistent failure.
In case it helps to know. The total project has the following specifications:
Must use a structure to store the following inventory data in a file:
item description
qty on hand
wholesale cost
retail cost
date added to inventory
will eventually use 3 separate functions to:
add records, display a requested record, change any record.
const int DESC_SIZE = 15;
const int DATE_ADDED_SIZE = 8;
// declare a structure for an inventory record
struct InventoryRecord
{
char desc[DESC_SIZE];
int qtyOnHand;
double wholesaleCost;
double retailCost;
char dateAdded[DATE_ADDED_SIZE];
};
int main()
{
InventoryRecord invRec; // to hold inventory record
char choice = 'n'; // to hold response Y or N
// open a file for binary output.
fstream invFile("products.dat", ios::out | ios::app |ios::binary);
do
{
// get data about a product
cout << "Enter Inventory Data:\n\n\n";