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. Menu Option 2 should do the following tasks Prompt for a customer number to change. Enter the address information ( Street 1 & 2, City , State and Zipcode) Find the record for the customer number and replace the address information. Menu Option 3 should do the following tasks Reset the file pointer to the beginning of the file. For each record in the file, display the customer number, customer name and all address information. The items isDeleted and newLine are not to be displayed. Modify the following code: #include #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 add_data(int no_of_records, ofstream& fout) { struct Customers c; cout << "Enter the Customer data:" << endl; cout << "Name:"; cin >> c.name; cout << "Street Address 1:"; cin >> c.streetAddress_1; cout << "Street Address 2:"; cin >> c.streetAddress_2; cout << "City:"; cin >> c.city; cout << "State:"; cin >> c.state; cout << "Zip Code:"; cin >> c.zipCode; cout << endl; c.customerNumber = no_of_records; c.isDeleted = 'N'; c.newLine = '\n'; cout << "Customer Details are:" << endl; cout << "\tCustomer Number:" << c.customerNumber << endl; cout << "\tName:" << c.name << endl; cout << "\tStreet Address 1:" << c.streetAddress_1 << endl; cout << "\tStreet Address 2:" << c.streetAddress_2 << endl; cout << "\tCity:" << c.city << endl; cout << "\tState:" << c.state << endl; cout << "\tZipCode:" << c.zipCode << endl; fout.write(reinterpret_cast(&c), sizeof(Customers)); } void display_menu(int no_of_records, ofstream& fout) { string choice; cout << "Choose the task:" << endl; cout << "1. Add Data" << endl; cout << "X. Exit Program" << endl; cin >> choice; if (choice == "1") { add_data(no_of_records, fout); no_of_records = no_of_records + 1; display_menu(no_of_records, fout); } else if (choice == "X") { cout << "Done!" << endl; } else { cout << "Invalid Choice!!! Please try again." << endl; display_menu(no_of_records, fout); } } int main() { ofstream fout; fout.open("Customers.dat"); if (!fout) { cout << "Cannot open file!" << endl; return 1; } fout.close(); fout.open("Customers.dat", ios::binary | ios::app); if (!fout) { cout << "Cannot open file to append!" << endl; return 1; } int no_of_records = 0; display_menu(no_of_records, fout); fout.close(); ifstream inputFile; inputFile.open("Customers.dat", ios::binary); if (!inputFile) { cout << "Cannot open the file to read!" << endl; return 1; } Customers c; cout << "Customer Details are:" << endl; while (inputFile.read((char*)&c, sizeof(Customers))) { cout << "\tCustomer Number:" << c.customerNumber << endl; cout << "\tName:" << c.name << endl; cout << "\tStreet Address 1:" << c.streetAddress_1 << endl; cout << "\tStreet Address 2:" << c.streetAddress_2 << endl; cout << "\tCity:" << c.city << endl; cout << "\tState:" << c.state << endl; cout << "\tZipCode:" << c.zipCode << endl; cout << endl; } return 0; }
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
- 1. Add Data
-
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.
-
Menu Option 2 should do the following tasks
-
Prompt for a customer number to change.
-
Enter the address information ( Street 1 & 2, City , State and Zipcode)
-
Find the record for the customer number and replace the address information.
-
-
Menu Option 3 should do the following tasks
-
Reset the file pointer to the beginning of the file.
-
For each record in the file, display the customer number, customer name and all address information. The items isDeleted and newLine are not to be displayed.
-
Modify the following code:
#include<iostream>
#include<fstream>
#include<string>
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 add_data(int no_of_records, ofstream& fout) {
struct Customers c;
cout << "Enter the Customer data:" << endl;
cout << "Name:";
cin >> c.name;
cout << "Street Address 1:";
cin >> c.streetAddress_1;
cout << "Street Address 2:";
cin >> c.streetAddress_2;
cout << "City:";
cin >> c.city;
cout << "State:";
cin >> c.state;
cout << "Zip Code:";
cin >> c.zipCode;
cout << endl;
c.customerNumber = no_of_records;
c.isDeleted = 'N';
c.newLine = '\n';
cout << "Customer Details are:" << endl;
cout << "\tCustomer Number:" << c.customerNumber << endl;
cout << "\tName:" << c.name << endl;
cout << "\tStreet Address 1:" << c.streetAddress_1 << endl;
cout << "\tStreet Address 2:" << c.streetAddress_2 << endl;
cout << "\tCity:" << c.city << endl;
cout << "\tState:" << c.state << endl;
cout << "\tZipCode:" << c.zipCode << endl;
fout.write(reinterpret_cast<char*>(&c), sizeof(Customers));
}
void display_menu(int no_of_records, ofstream& fout) {
string choice;
cout << "Choose the task:" << endl;
cout << "1. Add Data" << endl;
cout << "X. Exit Program" << endl;
cin >> choice;
if (choice == "1") {
add_data(no_of_records, fout);
no_of_records = no_of_records + 1;
display_menu(no_of_records, fout);
}
else if (choice == "X") {
cout << "Done!" << endl;
}
else {
cout << "Invalid Choice!!! Please try again." << endl;
display_menu(no_of_records, fout);
}
}
int main() {
ofstream fout;
fout.open("Customers.dat");
if (!fout) {
cout << "Cannot open file!" << endl;
return 1;
}
fout.close();
fout.open("Customers.dat", ios::binary | ios::app);
if (!fout) {
cout << "Cannot open file to append!" << endl;
return 1;
}
int no_of_records = 0;
display_menu(no_of_records, fout);
fout.close();
ifstream inputFile;
inputFile.open("Customers.dat", ios::binary);
if (!inputFile) {
cout << "Cannot open the file to read!" << endl;
return 1;
}
Customers c;
cout << "Customer Details are:" << endl;
while (inputFile.read((char*)&c, sizeof(Customers))) {
cout << "\tCustomer Number:" << c.customerNumber << endl;
cout << "\tName:" << c.name << endl;
cout << "\tStreet Address 1:" << c.streetAddress_1 << endl;
cout << "\tStreet Address 2:" << c.streetAddress_2 << endl;
cout << "\tCity:" << c.city << endl;
cout << "\tState:" << c.state << endl;
cout << "\tZipCode:" << c.zipCode << endl;
cout << endl;
}
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images