Question: 1. Modify program below to open the file Customers.dat so that all data is written to the end of the file AND to allow the
1. Modify program below 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.
Deliverable is a working CPP file and psuedoCode for the getLargestCustomerNumber method.
#include // header file for input output using cout, cin etc
#include // header file for file handling
using namespace std;
const int NAME_SIZE = 20; // constant for size of name
const int STREET_SIZE = 30; // // constant for size of street address
const int CITY_SIZE = 20; // constant for size of city
const int STATE_CODE_SIZE = 3; // constant for size of statecode
// structure for customers will all the required fields
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;
};
int main()
{
// creating a variable/object of customers structure with name c
struct Customers c;
// because customer number is incremented each time and stored in the CustomerNumber in structure, so creating a variable cus_num.
// initializing it to 0
long cus_num=0;
// choice whether user wants to continue with more records
char choice;
// using fstream we have created a file object with the name f
// customers.dat -> name of the file
// out -> because we are using for writing in file
// binary -> it is a binary file that we are opening
fstream f("customers.dat",ios::out|ios::binary|ios::app);
do
{
// incrementing cus_num by 1 and assingning to c.CustomerNumber
cus_num++;
c.customerNumber=cus_num;
f<
cout<
cin>>c.name;
f<
fflush(stdin);
cout<
cin>>c.streetAddress_1;
f<
fflush(stdin); // to clear the input buffer
cout<
cin>>c.streetAddress_2;
f<
fflush(stdin);
cout<
cin>>c.city;
f<
fflush(stdin);
cout<
cin>>c.state;
f<
fflush(stdin);
cout<
cin>>c.zipCode;
f<
c.isDeleted='N'; // storing 'N' in c.isDeleted and storing it in file
f<
c.newLine=' '; // storing ' ' in c.newLine and storing it in file
f<
cout<
cin>>choice;
}while(choice=='y' || choice=='Y'); // loop again if user response is y or Y
// come out of the loop if user response is n or N
cout<
// close the file object
f.close();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
