#include #include using namespace std; const int SIZE = 51; //array size const int NUM_REC = 10; struct Inventory //declare structure to hold related variables { char itemDescription[SIZE]; int quantityOnHand; double wholesaleCost; double retailPrice; int dateAdded; }; int main() { Inventory record = { " ", 0, 0.0, 0.0, 000000 }; //create empty structure char again; fstream store("store.dat", ios::out | ios::binary); //open file for binary output for(int count = 0; count < NUM_REC; count++) store.write(reinterpret_cast(&record), sizeof(record)); //write blank record store.close(); store.open("store.dat", ios::in | ios::binary); //open file for binary input if(!store) //test for file error { cout << "Error opening file. Program aborting.\n"; return 0; } cout << endl; //cout << "Here are the records in the file: \n"; store.read(reinterpret_cast(&record), sizeof(record)); //read and display records while (!store.eof()) { cout << endl; cout << "Item Description: "; cout << record.itemDescription << endl; cout << "Quantity on hand: "; cout << record.quantityOnHand << endl; cout << "Current wholesale cost: $"; cout << record.wholesaleCost << endl; cout << "Current retail price: $"; cout << record.retailPrice << endl; cout << "Dated added to inventory (yymmdd): "; cout << record.dateAdded << endl; store.read(reinterpret_cast(&record), sizeof(record)); } cout << "That's all the data in the file.\n"; store.close(); //close the file long recNum; store.open("store.dat", ios::in | ios::out | ios::binary); //open file for binary output/input/edits if(!store) //test for file error { cout << "Error opening file. Program aborting.\n"; return 0; } cout << endl; cout << "Would you like to edit a record: "; cin >> again; if(again == 'y' || again == 'Y') { do { cout << "Which record do you want to edit: "; cin >> recNum; store.seekg(recNum * sizeof(record), ios::beg); //set read location to start of record store.read(reinterpret_cast(&record), sizeof(record)); //read record to edit cout << endl; cout << "Item Description: "; cout << record.itemDescription << endl; cout << "Quantity on hand: "; cout << record.quantityOnHand << endl; cout << "Current wholesale cost: $"; cout << record.wholesaleCost << endl; cout << "Current retail price: $"; cout << record.retailPrice << endl; cout << "Dated added to inventory (yymmdd): "; cout << record.dateAdded << endl; cout << endl; cout << "Please enter the new data.\n"; cout << "Item Description: "; //get user input cin.ignore(); cin.getline(record.itemDescription, SIZE); cout << "Quantity on hand: "; cin >> record.quantityOnHand; while(record.quantityOnHand < 0) { cout << "Please enter quantity amount equal \n"; cout << "to or greater than 0: "; cin >> record.quantityOnHand; }; cout << "Current wholesale cost: "; cin >> record.wholesaleCost; while(record.wholesaleCost < 0) { cout << "Please enter Wholesale Cost equal \n"; cout << "to or greater than 0: "; cin >> record.wholesaleCost; }; cout << "Current retail price: "; cin >> record.retailPrice; while(record.retailPrice < record.wholesaleCost) { cout << "Please enter Retail Price equal to or \n"; cout << "greater than Wholesale Cost: "; cin >> record.retailPrice; }; cin.ignore(); cout << "Dated added to inventory (yymmdd): "; cin >> record.dateAdded; while(record.dateAdded < 160101) { cout << "Date added must be later than Jan 1, 2016.\n"; cout << "Please re-enter date added to inventory (yymmdd): "; cin >> record.dateAdded; }; store.seekp(recNum * sizeof(record), ios::beg); //go back to beginning of record to edit store.write(reinterpret_cast(&record), sizeof(record)); //overwrite with new data cout << "Do you want to edit another record.\n"; cin >> again; cin.ignore(); }while(again == 'y' || again == 'Y'); } store.close(); //close file store.open("store.dat", ios::in | ios::binary); //open file for binary input if(!store) //test for error { cout << "Error opening file. Program aborting.\n"; return 0; } cout << "Here are the updated records in the file: \n"; store.read(reinterpret_cast(&record), sizeof(record)); //read file for updated records while (!store.eof()) { cout << endl; cout << "Item Description: "; cout << record.itemDescription << endl; cout << "Quantity on hand: "; cout << record.quantityOnHand << endl; cout << "Current wholesale cost: $"; cout << record.wholesaleCost << endl; cout << "Current retail price: $"; cout << record.retailPrice << endl; cout << "Dated added to inventory (yymmdd): "; cout << record.dateAdded << endl; cout << "\nPress the Enter key to see the next record.\n"; cin.get(again); store.read(reinterpret_cast(&record), sizeof(record)); } cout << "That's all the data in the file.\n"; store.close(); return 0; }