Question: . Modify this program to open the file Customers.dat so that all data is written to the end of the file AND to allow the
. Modify this program to open the file "Customers.dat" so that all data is written to the end of the file AND to allow the file to be read.
2. 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 - do not assume the last record in the file is the largest number. Use this number as the base when adding new customer data.
3. Display the customer number calculated for the customer number that is receiving input.
4. The program needs to create the Customers.dat file if it does not exist.
5. The program should be able to start multiple times and add data with increasing customerNumbers. There should be no duplicated customer numbers.
#include
#include
using namespace std;
const int NAME_SIZE = 20;
const int STREET_SIZE = 30;
const int CITY_SIZE = 20;
const int 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 readData(){
int size = 0;
int custNumber = 0;
char option;
fstream file;
file.open("customers.dat", ios::out | ios::binary);
do{
Customers customer;
customer.customerNumber = custNumber;
cout<<"Customer Name: ";
cin.getline(customer.name, NAME_SIZE+1);
cout<<"street address1: ";
cin.getline(customer.streetAddress_1, STREET_SIZE + 1);
cout<<"street address 2: ";
cin.getline(customer.streetAddress_2, STREET_SIZE + 1);
cout<<"city: ";
cin.getline(customer.city, CITY_SIZE + 1);
cout<<"state: ";
cin.getline(customer.state, STATE_CODE_SIZE + 1);
cout<<"zip code: ";
cin>>customer.zipCode;
customer.isDeleted = 'N';
customer.newLine = ' ';
file.write((char *)&customer, sizeof(customer));
custNumber++;
cout<<"Do you want to continue! y/n: ";
cin>>option;
option = tolower(option);
cin.ignore();
}while(option == 'y');
file.close();
return;
}
int main(){
readData();
return 0;
}
"Customers.dat"
The program needs to create the Customers.dat file if it does not exist.
const int NAME_SIZE = 20; const int STREET_SIZE = 30; const int CITY_SIZE = 20; const int 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; };
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
