Question: Modify this program to open the file Customers.dat so that all data can be read, and data written or appended to this file. Add an
-
Modify this program to open the file "Customers.dat" so that all data can be read, and data written or appended to this file.
-
Add an option 2 and 3 to the menu so that the menu appears as below:Menu options '2' and '3' should now be considered a valid choice.
- 1. Add Data 2. Update Data 3. Display all data X. Exit Program
-
Create a method called "getLargestCustomerNumber" and call it after the "Customers.dat" file has been opened. Read all the existing customerNumbers and determine the largest customerNumber. Use this number as the base when adding new customer data.
-
The program should be able to start multiple times and add data with increasing customerNumbers. There should be no duplicated customer numbers.
This is my assignment, I am stuck on one part and need help on that one part. If the 3rd option is chosen, all the data is displayed. along with their customer number. The program should keep track of the customer number even if the program is stopped and restarted. But when i display the data, all my customers have the same customer number...that being #1. How can i get them to have gradual customer numbers 1,2,3 etc. even if the program was restarted. Thats my only issue right now and would appreciate any help i could get! Here is my code.
#include
const int NAME_SIZE = 20, STREET_SIZE = 30, CITY_SIZE = 20, STATE_CODE_SIZE = 3;
struct Customers { long customerNumber; char name[NAME_SIZE]; char streetAddress_1[STREET_SIZE]; char streetAddress_2[STREET_SIZE]; char city[CITY_SIZE]; char state[STATE_CODE_SIZE]; int zipCode; char isDeleted; char newLine; };
void displayMenu(); int getLargestCustomerNumber(Customers); void changeRecord(Customers); void displayRecord(Customers);
int main() { Customers info; fstream dataFile; char choice; char again; info.customerNumber = 0; info.isDeleted = 'N'; info.newLine = ' ';
dataFile.open("Customer.dat", ios::in | ios::out | ios::binary | ios::app); getLargestCustomerNumber(info);
do { displayMenu(); cout << "Enter your choice: "; cin >> choice; cin.ignore();
if (choice == '1') { cout << "Name: "; cin.getline(info.name, NAME_SIZE); cout << "Address 1: "; cin.getline(info.streetAddress_1, STREET_SIZE); cout << "Address 2: "; cin.getline(info.streetAddress_2, STREET_SIZE); cout << "City: "; cin.getline(info.city, CITY_SIZE); cout << "State: "; cin.getline(info.state, STATE_CODE_SIZE); cout << "Zip Code: "; cin >> info.zipCode;
dataFile.write(reinterpret_cast
else if (choice == '2') { changeRecord(info); }
else if (choice == '3') { displayRecord(info); cout << "That is all the data in the file! "; }
else if (choice == 'x' || choice == 'X') { dataFile.close(); cout << "Done! "; break; }
else { cout << "Invalid option!"; }
cout << "Would you like to try again: "; cin >> again;
} while (again == 'Y' || again == 'y');
dataFile.close(); cout << "Data has been loaded to file!";
return 0; }
void displayMenu() { cout << "MENU "; cout << "---- "; cout << "1. Add Data "; cout << "2. Update Data "; cout << "3. Display All Data "; cout << "X. Exit Program "; }
int getLargestCustomerNumber(Customers info) { int customer_Number = -1;
FILE* ptr = fopen("Customer.dat", "rb");
if (ptr == NULL) return -1; while (!feof(ptr)) { fread(&info, sizeof(Customers), 1, ptr);
if (info.customerNumber > customer_Number) customer_Number = info.customerNumber; } fclose(ptr); return customer_Number; }
void changeRecord(Customers info) { int customer_Number;
fstream dataFile("Customer.dat", ios::in | ios::out | ios::binary);
cout << "Which customer number do you want to change: "; cin >> customer_Number;
dataFile.seekg(customer_Number * sizeof(info), ios::beg); dataFile.read(reinterpret_cast
cout << "Name: " << info.name << endl; cout << "Address 1: " << info.streetAddress_1 << endl; cout << "Address 2: " << info.streetAddress_2 << endl; cout << "City: " << info.city << endl; cout << "State: " << info.state << endl; cout << "Zip Code: " << info.zipCode << endl;
cout << " Change address 1 if needed: "; cin.getline(info.streetAddress_1, STREET_SIZE); cin.ignore();
cout << "Change address 2 if needed: "; cin.getline(info.streetAddress_2, STREET_SIZE); cin.ignore();
cout << "Change city if needed: "; cin.getline(info.city, CITY_SIZE); cin.ignore();
cout << "Change state if needed: "; cin.getline(info.state, STATE_CODE_SIZE); cin.ignore();
cout << "Change Zip Code if needed: "; cin >> info.zipCode;
dataFile.seekp(customer_Number * sizeof(info), ios::beg); dataFile.write(reinterpret_cast
dataFile.close(); }
void displayRecord(Customers info) { fstream dataFile("Customer.dat", ios::in | ios::binary);
dataFile.read(reinterpret_cast
while (!dataFile.eof()) { cout << "Customer #" << info.customerNumber + 1 << endl; cout << "Name: " << info.name << endl; cout << "Address 1: " << info.streetAddress_1 << endl; cout << "Address 2: " << info.streetAddress_2 << endl; cout << "City: " << info.city << endl; cout << "State: " << info.state << endl; cout << "Zip Code: " << info.zipCode << endl; dataFile.read(reinterpret_cast
dataFile.close(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
