//File : fileOps.cpp //Author : Andre Long //Due : September 21, 2004 //Description : This is program challenge 11 out of chapter 12 // It will use structures to create records where the user can add new records to a file // Display any record in the file // Change any record in the file #include #include #include using namespace std; struct inventory { string description; int quantity; float wholesaleCost; float retailCost; string date; }; int countOfRecords(inventory record); int menu(); int add(inventory record, fstream &, int *recordCounter); void display(inventory record, fstream &, int recordCounter); void displayAll(inventory record, fstream &, int recordCounter); void modify(inventory record, fstream &, int recordCounter); int main() { int choice; int recordCounter=0; cout<<"Welcome to the company store inventory program"<<"\n"; cout<<"If the input file does not exists or is not found, the program will create it"<<"\n"; inventory record = {"store item", 0, 0, 0, "01/01/2000"}; recordCounter = countOfRecords(record); cout<<"\n"; cout<<"recordCounter = "<4); inAndOut.close(); return 0; } int countOfRecords(inventory record) { int recordCounter=0; fstream inAndOut("inventoryData.txt", ios::in | ios::out | ios::app| ios::binary); if(inAndOut.is_open() ==false) { cout<<"There was an error reading the data file"<<"\n"; } else { inAndOut.seekg(0 *sizeof(record), ios::beg); inAndOut.read(reinterpret_cast(&record), sizeof(record) ); while(!inAndOut.eof()) { recordCounter = recordCounter+1; inAndOut.read(reinterpret_cast(&record), sizeof(record) ); } } inAndOut.close(); return recordCounter; } int menu() { int choice; cout<<"Please make a menu choice for the task you which to accomplish"<<"\n"; cout<<"\n"; cout<<" 0: Quit this program"<<"\n"; cout<<" 1: Add new records to the file"<<"\n"; cout<<" 2: Display any record in the file"<<"\n"; cout<<" 3: Display all records in the file"<<"\n"; cout<<" 4: Change any record in the file"<<"\n"; cout<<"\n"; cout<<"\n"; cout<<"Enter menu choice now "; cin>>choice; cout<<"\n"; if (choice <0 || choice > 4) cout<<"You must select a numeric choice from the menu (0, 1, 2, 3, 4)"<<"\n"; return choice; } int add(inventory record, fstream &inAndOut, int *recordCounter) { inAndOut.seekp(0L, ios::end); cout<<"Enter the item descripton "; cin.ignore(); getline(cin, record.description); cout<<"\n"; cout<<"Enter the quantity on hand "; cin>>record.quantity; cout<<"\n"; cout<<"Enter the wholesale cost "; cin>>record.wholesaleCost; cout<<"\n"; cout<<"Enter the retail cost "; cin>>record.retailCost; cout<<"\n"; cout<<"Enter the date added to the inventory "; cin.ignore(); getline(cin, record.date); cout<<"\n"; inAndOut.write(reinterpret_cast(&record),sizeof(record)); *recordCounter = *recordCounter+1; } void display(inventory record, fstream &inAndOut, int recordCounter) { int recNum; cout<<"Which particular file would you like to see? "; cin>>recNum; cout<<"\n"; if ( (recNum > (recordCounter-1) ) || (recNum < 0 )) { cout<<"That record does not exist"<<"\n"; } else { inAndOut.seekg(recNum *sizeof(record), ios::beg); inAndOut.read(reinterpret_cast(&record), sizeof(record)); cout<<"For record number "<(&record), sizeof(record) ); cout<<"Record Number: "<>recNum; if ( (recNum > (recordCounter-1) ) || (recNum < 0 )) { cout<<"That record does not exist and can't be edited! "<<"\n"; } else { inAndOut.seekg(recNum *sizeof(record), ios::beg); inAndOut.read(reinterpret_cast(&record), sizeof(record)); cout<<"The current item description: "<>record.quantity; cout<<"\n"; cout<<"The new Whole Sale Price is: "; cin>>record.wholesaleCost; cout<<"\n"; cout<<"The new Retail Price is: "; cin>>record.retailCost; cout<<"\n"; cout<<"The new date added to inventory is: "; cin.ignore(); getline(cin, record.date); cout<<"\n"; inAndOut.seekp(recNum *sizeof(record), ios::beg); inAndOut.write(reinterpret_cast(&record),sizeof(record)); } }