In: Computer Science
code from assignment 1
#include
#include
using namespace std;
const int nameLength = 20;
const int stateLength = 6;
const int cityLength = 20;
const int streetLength = 30;
int custNomber = -1;
// Structure
struct Customers
{
long customerNumber;
char name[nameLength];
char state[stateLength];
char city[cityLength];
char streetAddress1[streetLength];
char streetAddress2[streetLength];
char isDeleted = 'N';
char newLine = '\n';
int zipCode;
};
int main()
{ ofstream file;
file.open("Customers.dat", fstream::binary | fstream::out);
char go;
long entries = 0;
struct Customers data;
do
{
data.customerNumber = entries;
file << data.customerNumber << "";
cout << "customerNumber";
cin >> data.customerNumber;
cout << "Name: ";
cin >> data.name, nameLength;
cout << "Address line 1: ";
cin >> data.streetAddress1, streetLength;
cout << "Address line 2: ";
cin >> data.streetAddress2, streetLength;
cout << "City: ";
cin >> data.city, cityLength;
cout << "State: ";
cin >> data.state, stateLength;
cout << "Zip Code: ";
cin >> data.zipCode;
file.write((const char*)&data, sizeof(data));
cout << "Do you want to enter another record? ";
cin >> go;
} while (go == 'Y' || go == 'y');
{
entries++;
file.close();
}
}
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.
Deliverable is a working CPP file and psuedoCode for the getLargestCustomerNumber method.
told to provide customer.dat file, the file is supposed to be create within the program
I have written down the complete code with minor changes. Please check it out.
CODE:
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
const int NAME_SIZE = 20;
const int STREET_SIZE = 30;
const int CITY_SIZE = 20;
const int STATE_CODE_SIZE = 6;
// Structure
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 = 'N';
char newLine = '\n';
};
void getLargestCustomerNumber(ifstream &testFile, int counter)
{
string line;
int i=0, max[counter];
while(getline(testFile, line))
{
stringstream ss(line);
ss >> max[i];
i++;
}
int maxNum = INT_MIN;
for(int x=0; x<counter; x++)
{
if (max[x]>maxNum)
maxNum = max[x];
}
cout<<"Maximum Customer Number: "<<maxNum;
}
int main()
{
struct Customers data;
char ch;
int counter=0;
ofstream myfile;
myfile.open("Customers.dat", ios::binary | ios::out);
if (!myfile)
cout << "File Not Found." << endl;
else
{
do
{
counter++;
cout<<"enter customer number: ";
cin>>data.customerNumber;
cout<<"enter name: ";
cin>>data.name;
cout<<"enter streetAddress_1: ";
cin>>data.streetAddress_1;
cout<<"enter streetAddress_2: ";
cin>>data.streetAddress_2;
cout<<"enter city: ";
cin>>data.city;
cout<<"enter state: ";
cin>>data.state;
cout<<"enter zip code: ";
cin>>data.zipCode;
myfile << data.customerNumber << "\t" << data.name << "\t" << data.streetAddress_1 << "\t" << data.streetAddress_2
<< "\t" << data.city << "\t" << data.state << "\t" << data.zipCode << data.newLine;
cout << "Do you want to enter another record? (y or Y): ";
cin >> ch;
}
while(ch == 'Y' || ch == 'y');
}
myfile.close();
cout<< "Reading The (Customers.dat) file .............\n";
ifstream testFile("Customers.dat");
string line;
getLargestCustomerNumber(testFile, counter);
return 0;
}
Hope this helps. But if you want me to make any changes, please mention in the comments.
Keep Coding....Good Luck :)