Question

In: Computer Science

code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int...

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

Solutions

Expert Solution

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 :)


Related Solutions

#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT