Question: C++ please, I have to fix the errors and get the correct output: Land borders need to keep track of the Cars passing through when
C++ please, I have to fix the errors and get the correct output:
Land borders need to keep track of the Cars passing through when they come into the country.
For the Cars Module:
In the module, create a structure called Car to hold the following information for a passing Car.
- License Plate of the Car; a Character cString capable of holding a maximum of 8 characters (8+1 for null byte)
- Make and model of the Car; a character pointer to hold the make and model of the car dynamically as a cString.
Although a "make and model" of a Car is not typically more than 60 characters, to conserve memory, after reading the make and model in a local cString of 60 characters(function scope), we will store it in a dynamically allocated memory to the same size of the entry.
- Time; kept in an integer in military format (ie. 30 is 0:30 AM and 1325 is 1:25 PM)
Have this structure in Car.h
Dynamic Array of Cars
In Car.cpp create a pointer to a Car structure.
This pointer will hold a dynamic array of Car structures with an initial size (that we call allocation size). As cars pass the border the elements of the dynamic array will be set to the information of the car until the array is full. In this case, a new array of dynamic cars with more memory will be allocated (the old size + allocation size) to replace the old array.
End of the day report
The execution of the programs starts at 0:01 in the morning and ends at midnight.
When the program ends, a time-stamped report will be generated for all the cars that passed the border that day. Before the program ends all the memory allocated during execution will be deallocated (deleted);
Create the following four variables in Car.cpp to be used for memory management and tracking:
- Car Pointer; A Car pointer to hold the dynamic array of cars (We call this "The Car Pointer" in this text)
- Allocation size; an integer to hold the memory allocation and expansion size when needed
When testing you set this to be a small number (like 2) so you can easily test your memory resizing.
- Number of Cars; an integer to hold the number of cars currently in the dynamic array of cars pointed by The Car Pointer
- Car Array Size; an integer to hold the current allocation size of the array. This value is the maximum size that the Number Of Cars can grow up to (before reallocation for expansion).
void VBPLE_Title();
Prints the following message:
Vehicle Border Passing Log Entry Enter the data in the following format: Make and model,LicensePlate,MilitaryTimeExit the program by entering the following: X,
and goes to new line.
void initialize(int allocSize);
Initializes the four Car.cpp variables as follows:
- sets the number of cars to zero
- sets the Car Array Size and the Allocation Size to the argument allocSize
- dynamically allocates an array of Cars to the size of Allocation Size and keeps the address in The Car Pointer
void deallocate(Car& C);
Deletes the allocated memory pointed by the make and model pointer in the Car structure, then set it to nullptr.
bool read(Car& C);
Overload the Read Function: Read the information of a car passing the border in a comma-separated format from the console and dynamically hold the make an model in the make and model pointer of the Car structure. Return true if a Car is read from the console or return false if "X," is entered instead.
No validation is done by this function on the data.
- create a boolean flag and set it to false
- create a local cString to hold 60 Characters to read the make and model
- call the cStrTools read function to read the make and model up to 60 characters or the comma character (',');
- if the make and model is not equal to "X" then
- set the boolean flag to true
- allocate a dynamic array of characters pointed by the make and model of the Car structure to the size of the make and model red from the console (plus the null byte) and copy the make and model into it.
- read the license plate up to 8 characters or up to the comma character (',');
- read the integer value and store it in the time variable of the Car structure.
- return the boolean flag.
void print(const Car& C);
print the car in the following format: Time: Make and model, license plate void record(const Car& C); Records the passage of a car by adding its information to the Car Array as follows: See the illustration bellow: void endOfDay() This function is called at the end of the program. Code: Car.h #ifndef CARS_H #define CARS_H struct Car { char licensePlate[9]; char* makeandmodel; //maybe int Time; }; namespace sdds { void VBPLE_Title(); void initialize(int allocSize); void deallocate(Car& C); bool read(Car& C); void print(const Car& C); void record(const Car& C); void endOfDay(); void read(char* cString, unsigned int maxSize, char delimiter); } #endif Car.cpp: #define _CRT_SECURE_NO_WARNINGS #include //prints out the title void VBPLE_Title() { cout << "Vehicle Border Passing Log Entry" << endl; cout << "Enter the data in the following format:" << endl; cout << "Make and model,LicensePlate,MilitaryTime //initalizes the values: void initialize(int allocSize) { noOfCars = 0; allocation_size = allocSize; carArraySize = allocSize; car_ptr = new Car[allocation_size]; //dynamically allocates data } //deallocates make and model void deallocate(Car& C) { delete[] C.makeandmodel; C.makeandmodel = nullptr; } bool read(Car& C) { bool flag = false; char cString[61]; char* temp; int i; int m = strLen(C.makeandmodel); while (1) { read(cString, 61, ','); if (C.makeandmodel != "X") { flag = true; C.makeandmodel* = new Car[m + 1]; for (i = 0; i < 375; i++) { temp[i] = C.makeandmodel[i]; } read(C.licensePlate, 9, ','); read(C.Time, 4, ','); } } return flag; } void print(const Car& C) { cout << "Time: " << C.Time << " Make and Model," << C.makeandmodel << " license plate" << endl; } void record(const Car& C) { int* revised; int i; if (noOfCars == carArraySize) { revised = new Car[allocation_size + carArraySize]; for (i = 0; i < 375; i++) { revised[i] = car_ptr[i]; } delete[] car_ptr; car_ptr[i] = revised[i]; } } void endOfDay() { int i; for (i = 0; i < noOfCars; i++) { print(car_ptr[i]); } delete[] car_ptr; car_ptr = nullptr; } } #include "Car.h" using namespace sdds; int main() { Car C; VBPLE_Title(); initialize(2); // set this later to 5 while (read(C)) { record(C); } endOfDay(); } /* Copy and paste all the lines into the terminal instead of typing them. Pasting in the terminal is usually done only by right clicking the mouse. Toyota C-HR,VYEEDG,0001 Alfa Romeo Giulia,OVQXIQ,0004 Buick Encore GX,ZIQSUF,0007 Jeep Gladiator,YFQOSQ,1010 Chevrolet Impala,MWPBNW,1013 Volkswagen Atlas Cross Sport,ESIJDW,1116 Toyota Prius Prime,AECYJV,1319 Lexus RZ,QONGCT,1622 Tesla Model S,MCGSVD,2225 X,
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
