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 <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
#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 <cmath>using namespace std; const int A_CONSTANT = 3; void functionA(int a[], int aNumber);...
#include <iostream> #include <cmath>using namespace std; const int A_CONSTANT = 3; void functionA(int a[], int aNumber); void functionB(int a[], int anotherNumber); void functionC(const int anArray[], int aNumber); void functionD(int& sum); int functionE(double number); void functionF(int n); int main( ){ int production[A_CONSTANT]; cout << "This program displays a graph showing\n" << "production for each factory in the company.\n"; functionA(production, A_CONSTANT); functionB(production, A_CONSTANT); functionC(production, A_CONSTANT); return 0;} void functionA(int a[], int aNumber){ for (int someNumber = 1;someNumber <= aNumber; someNumber++) { cout...
in C++, #include<iostream> using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven...
in C++, #include<iostream> using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven (int b[]); int main() { int arr[NUM]; // write a statement to call prepareArr to set values for the array // write a statement to call countEven and print the data returned for(int i = 0; i<NUM; i++) cout << arr[i] <<" "; cout <<endl; return 0; } void prepareArr(int a[]) { //randomly generate NUM integers in the range [0,99] and save them in...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)        cout << x << " ";    cout << endl; } void println(const vector<string>& v) {    for (const string& x : v)        cout << " "<< x << " ";    cout << endl; } int main() {    vector<int> v0;    cout << "An empty vector of integers: ";    println(v0);    vector<int> v1(3);    cout << "A...
Fill in the code only using pointer variables #include using namespace std; int main() { int...
Fill in the code only using pointer variables #include using namespace std; int main() { int longside; // holds longside (length) int wideside; // holds wideside(width) int total; // holds total (area) int *longsidePointer = nullpointer; // int pointer which will be set to point to length int *widthPointer = nullpointer; // int pointer which will be set to point to width cout << "Please input the longside of the rectangle" << endl; cin >> longside; cout << "Please input...
#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;...
Trace this code: 1) #include<iostream> using namespace std;    class Test {     int value; public:     Test(int...
Trace this code: 1) #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } =================================================================== 2) #include <iostream> using namespace std; int main() {                 int i, j;                 for (i = 1; i <= 3; i++)                 {                                 //print * equal to row number                                 for (j = 1; j <= i; j++)                                 {                                                 cout...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT