In: Computer Science
Create a program that will loop and prompt to enter the highlighted data items in the structure below. This is every item except customerNumber , isDeleted and newLine;
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;
};
Always set the item isDeleted to 'N' and
newline to '\n'. The item newLine
is a convenient item that is there to assist in viewing the
contents of the file using "type filename" the cmd window.
Notepad will show the binary chars and will not line up the data as expected. You may see some odd characters after the expected data for the character arrays. That is normal for C/C++.
The item customerNumber should start at 0 and increase by 1 for every record written.
Once the data in the structure is loaded, write it to the file
"Customers.dat" and prompt to continue. If the reply is to not
continue, close the file and exit.
The file "Customers.dat" must be opened in Binary mode.
1. 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.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
#include <iostream> // header file for input output using cout, cin etc
#include <fstream> // 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()
{
struct Customers c;
long cus_num = 0;
char choice;
fstream f("customers.dat", ios::out | ios::binary | ios::app);
do
{
cus_num++;
c.customerNumber = cus_num;
f << c.customerNumber << " "; // storing the Customer number in file
cout << endl
<< "Please enter your name:"; // asking user for name, storing in c.name structure
cin >> c.name;
f << c.name << " "; // storing name in file
fflush(stdin);
cout << endl
<< "Please enter street address 1:"; // asking user for street address 1, storing in c.StreetAddress1 structure
cin >> c.streetAddress_1;
f << c.streetAddress_1 << " "; // storing street address 1 in file
fflush(stdin); // to clear the input buffer
cout << endl
<< "Please enter street address 2:"; // asking user for street address 2, storing in c.StreetAddress2 structure
cin >> c.streetAddress_2;
f << c.streetAddress_2 << " "; // storing street address 2 in file
fflush(stdin);
cout << endl
<< "Please enter city:"; // asking user for city, storing in c.City structure
cin >> c.city;
f << c.city << " "; // storing city in file
fflush(stdin);
cout << endl
<< "Please enter state:"; // asking user for state, storing in c.State structure
cin >> c.state;
f << c.state << " "; // storing state in file
fflush(stdin);
cout << endl
<< "Please enter zip code:"; // asking user for zip code, storing in c.ZipCode structure
cin >> c.zipCode;
f << c.zipCode << " "; // storing zipcode in file
c.isDeleted = 'N'; // storing 'N' in c.isDeleted and storing it in file
f << c.isDeleted << " ";
c.newLine = '\n'; // storing '\n' in c.newLine and storing it in file
f << c.newLine;
cout << endl
<< "Do you want to continue with more records [y/n] :"; // Asking user if he wants to continue
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 << endl
<< endl
<< "The data is successfully stored in the file customers.dat";
// close the file object
f.close();
}