In: Computer Science
C++
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.
Must add comments to your program.
Imclude a pseudo Code for the getLargestCustomerNumber method.
PLEASE DO NOT use an already answered question here on CHEGG. unfortunately I've had that happen at many occasions
.......
P.S. : In the current program the file in not explicitly closed after my loop is done. Please fix that, and include the pseudo code for getLargestCustomerNumber method
I'll most certainly give an UPVOTE. I ALWAYS do with correct answers.
.......
.......
#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;
};
int main()
{
// loop variable
char repeat;
do
{
// New customers structure object
Customers c;
c.customerNumber = 0;
//opens file in binary mode
ofstream fp("Customers.dat", ios::out | ios::binary);
if (!fp)
{
cout << "File Cannot be opened" << endl;
return 0;
}
// Prompting and reading inputs
cout << "\nEnter Name: ";
cin.getline(c.name, NAME_SIZE);
cout << "\nEnter Address 1: ";
cin.getline(c.streetAddress_1, STREET_SIZE);
cout << "\nEnter Address 2: ";
cin.getline(c.streetAddress_2, STREET_SIZE);
cout << "\nEnter City: ";
cin.getline(c.city, CITY_SIZE);
cout << "\nEnter State: ";
cin >> c.state;
cout << "\nEnter Zip Code: ";
cin >> c.zipCode;
// Ignores rest of current line until '\n'
cin.ignore(numeric_limits::max(), '\n');
// Initializing isDeleted and newline
c.isDeleted = 'N';
c.newLine = '\n';
//increments customerNumber
c.customerNumber++;
//writes data to Customers.dat
// fp.write((char*)&c, strlen(Customers));
fp.write((char*)&c, sizeof(Customers));
cout << "\nEnter y/Y to continue or n/N to exit:
";
cin >> repeat;
cin.ignore(numeric_limits::max(), '\n');
// Quit if N/n is entered, otherwise loops
if (repeat == 'N' || repeat == 'n')
{
cout << " Exiting and closing file..........";
//closing the file
fp.close();
break;
}
} while (repeat == 'y' || repeat == 'Y');
return 0;
}
I have made chanegs to your code with required specs
Pseudocode for getLargestCustomerNumber
(code to copy)
#include<bits/stdc++.h>
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;
};
int getLargestCustomerNumber(ifstream &infile){
int cus_no = 0;
int rd;
while(infile>>rd){
cus_no=max(rd,cus_no);
}
infile.close();
return cus_no;
}
int main()
{
// loop variable
char repeat;
do
{
// New customers structure object
Customers c;
c.customerNumber = 0;
//opens file to read
ifstream infile("Customers.dat");
if (!infile)
{
cout << "File Cannot be opened. Creating new file" << endl;
infile.open("Customers.dat", ifstream::in | ifstream::out | ifstream::trunc);
}
int largestCustomerNumber = getLargestCustomerNumber(infile);
cout<<"getLargestCustomerNumber() -> "<<largestCustomerNumber<<endl;
// Prompting and reading inputs
cout << "\nEnter Name: ";
cin.getline(c.name, NAME_SIZE);
cout << "\nEnter Address 1: ";
cin.getline(c.streetAddress_1, STREET_SIZE);
cout << "\nEnter Address 2: ";
cin.getline(c.streetAddress_2, STREET_SIZE);
cout << "\nEnter City: ";
cin.getline(c.city, CITY_SIZE);
cout << "\nEnter State: ";
cin >> c.state;
cout << "\nEnter Zip Code: ";
cin >> c.zipCode;
// Ignores rest of current line until '\n'
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// Initializing isDeleted and newline
c.isDeleted = 'N';
c.newLine = '\n';
//increments customerNumber
c.customerNumber = largestCustomerNumber+1;
// //opens file to write, use std::ios_base::app to append
ofstream fp("Customers.dat", std::ios_base::app);
//writes data to Customers.dat
fp<<c.customerNumber<<endl;;
cout << "\nEnter y/Y to continue or n/N to exit: ";
cin >> repeat;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// Quit if N/n is entered, otherwise loops
if (repeat == 'N' || repeat == 'n')
{
cout << " Exiting and closing file..........";
//closing the file
fp.close();
break;
}
} while (repeat == 'y' || repeat == 'Y');
return 0;
}
code screenshot
There was no file named Customers.dat before execution
Sample Input/Output Screenshot 1
Sample Input/Output Screenshot 2
Contents of Customer.dat after above executions
Let me know in the comments if you have any doubts.
Do leave a thumbs up if this was helpful.