#include #include #include using namespace std; const int FILENAME = 21; class inventory { public: string item; int quantity, day, month, year; double wholesale, retail; }; inventory add_rec(); int menu(); void display_rec(inventory add_rec, char filename[FILENAME]); void single_rec(char filename[FILENAME], int record_num); int search_rec(inventory add_rec, char filename[FILENAME]); long byte_num(int); int num_elements(char filename[FILENAME]); int menu() { int num; system("cls"); cout << "Select a number for the choices below: " << endl; cout << "--------------------------------------" << endl; cout << "1. Add a record..." << endl; cout << "2. Display all records contents..." << endl; cout << "3. Display a single records contents..." << endl; cout << "4. Change any records contents..." << endl; cout << "5. End program." << endl <> num; return num; } inventory add_rec() { inventory record; char yesno; do { cout << endl; cout << "Enter a description of the item: "; cin >> record.item; cout << "Enter the quantity on hand: "; do{cin >> record.quantity;}while(record.quantity <= 0); cout << "Enter the wholesale cost: "; do{cin >> record.wholesale;}while(record.wholesale <= 0); cout << "Enter the retail cost: "; do{cin >> record.retail;}while(record.retail <= 0); cout << "Enter the data added to the inventory (XX XX XXXX) : "; do{cin >> record.month; if(record.month < 1 || record.month > 12) cout << "Invalid month!\t";}while(record.month <= 0 || record.month > 12); do{cin >> record.day; if(record.day < 1 || record.day > 31) cout << "Invalid day!\t";}while(record.day <= 0 || record.day > 31); do{cin >> record.year; if(record.year < 1960 || record.year > 2020) cout << "Invalid year!\t";}while(record.year <= 1960 || record.year > 2020); cout << "Would you like to enter another record? (y or n) "; cin >> yesno; }while(yesno == 'y' || yesno == 'Y'); return record; } void display_rec(inventory record, char filename[FILENAME]) { fstream dataFile; //char ch; dataFile.open(filename, ios::in | ios::binary); dataFile.read(reinterpret_cast(&record), sizeof(record)); do { cout << "Item description : " << record.item << endl; cout << "Quantity on hand : " << record.quantity << endl; cout << "Wholesale cost : " << record.wholesale << endl; cout << "Retail cost : " << record.retail << endl; cout << "Date added : " << record.day << "/" << record.month << "/" << record.year << endl << endl; //cout << "Press any key for next record." << endl << endl; //cin.get(ch); dataFile.read(reinterpret_cast(&record), sizeof(record)); }while(!dataFile.eof()); dataFile.close(); } int search_rec(char filename[FILENAME], char value[81]) { inventory record; fstream dataFile; int index = 0; int position = -1; bool found = false; int num_elements = 0; num_elements = num_elements(filename); dataFile.open(filename, ios::in | ios::binary); while((index < num_elements) && !found) { dataFile.seekg(byte_num(index), ios::beg); dataFile.read(reinterpret_cast(&record),sizeof(record)); if(strcmp(record.item,value) == 0) { found = true; position = index; } index++; } dataFile.close(); return position; } void single_rec(char filename[FILENAME], int record_num) { inventory record; fstream dataFile; dataFile.open(filename, ios::in | ios::binary); dataFile.seekg(byte_num(record_num), ios::beg); dataFile.read(reinterpret_cast(&record), sizeof(record)); cout << "Item description : " << record.item << endl; cout << "Quantity on hand : " << record.quantity << endl; cout << "Wholesale cost : " << record.wholesale << endl; cout << "Retail cost : " << record.retail << endl; cout << "Date added : " << record.day << "/" << record.month << "/" << record.year << endl << endl; dataFile.close(); return; } long byte_num(int record_num) { return sizeof(inventory) * record_num; } int num_elements(char filename[FILENAME]) { inventory record; fstream dataFile; int num_elements = 0; dataFile.open(filename, ios::in | ios::binary); while(!dataFile.eof()) { dataFile.read(reinterpret_cast(&record),sizeof(record)); num_elements++; } dataFile.close(); return num_elements; } int main() { inventory record; fstream dataFile; char filename[FILENAME] = "record.txt"; int num, position; char target[81]; do { num = menu(); switch(num) { case 1: cin.ignore(); dataFile.open(filename, ios::app | ios::out | ios::binary); record = add_rec(); dataFile.write(reinterpret_cast(&record),sizeof(record)); dataFile.close(); break; case 2: display_rec(record, filename); system("pause"); break; case 3: cout << "Enter the item's description to view: "; cin >> target; position = 0; position = search_rec(filename, target); if(position == -1) {cout << "Invalid record..." << endl;} else single_rec(filename, position); break; case 4: cout << "Enter the item's description to change: "; cin >> target; position = 0; if(position == -1) {cout << "Invalid record..." << endl;} else dataFile.open(filename, ios::in | ios::out | ios::binary); record = add_rec(); dataFile.seekp(position * sizeof(record), ios::beg); dataFile.write(reinterpret_cast(&record), sizeof(record)); dataFile.close(); break; default: cout << "Please enter a valid choice." << endl << endl; break; } }while(num != 5); return 0; }