Question: Problem 4.1 In this problem, we will read cars from a file, rather than typing them in from the keyboard. Do the steps in order

Problem 4.1

In this problem, we will read cars from a file, rather than typing them

in from the keyboard. Do the steps in order to build the solution to

this problem.

1. Copy and clean up code from problem 3.3

* Copy problem 3.3

* Change the name to Assignment 4, Problem 4.1

* Remove everything from the main function.

* Remove the execution results.

2. Modify the input function.

* Remove the & from the parameters in the function header, so they

are all values rather than references.

* Move the parameters from the function header and put them within

the input function, so they are now all local variables.

* Remove the parameters from the prototype for the input function,

so it matches the function header.

* At the bottom of the input function, declare a Car object named

temp using the constructor that takes the five parameters.

* Use the Car output function to print the Car.

* Call the input function from the main function with no arguments.

3. Create a file and use it for input. This is good because you will be

using the input many times.

?

Use the file named

cardata4.txt

( provided), containing the

following three lines of data:

Car CN 819481 maintenance false NONE

Car SLSF 46871 tank

true Memphis

Car AOK 156 tender true McAlester

* In the input function, declare an object of type ifstream named

inputFile, which we will use to read from the file.

* At the beginning of the code for the input function, open the

file. If the open fails, send a message to stderr and exit the

program.

* In all the reads within the input function, remove the user

prompt and read from the inputFile object, rather than reading

from the stdin object.

* In the input function declare a string named type. Read the Type

field before reading the ARR field. We do not need the Type

field yet, but we need to read it to get it out of the way.

*

Hint: You need to use getline when reading the destination.

using >> skips leading white space before reading the data.

getline does not skip this leading whitespace. So, before using

getline use the following code:

while(inputFile.peek() == ' ')

inputFile.get();

peek looks at the next character you are about to read. If it is

a space, get is used to read the space character, to get it out

of the way.

* Use a loop to read each line from the file. To do this use a

while loop including all the reading in the input function, as

well building and output of the Car.

Hint: you can do this with the following while statement:

while(inputFile.peek() != EOF)

The peek function will return EOF is there is no next character.

* At the bottom of the input function, close the file.

Order of functions in the code:

1. main

2. Car member functions

1. constructors in the order

1. default constructor

2. copy constructor

3. other constructors

2. output

3. setUp

3. operator== with Car parameters

4. input

Put an eye catcher before the beginning of each function, class, and the

global area:

// class name function name comment(if any)

*************************************************

Problem 4.2

Copy the solution for problem 4.1

Copy the following operator= overload member function that returns the

left hand operator by reference:

// Car operator= **************************************************

Car & Car::operator=(const Car & carB)

{

reportingMark = carB.reportingMark;

carNumber = carB.carNumber;

kind = carB.kind;

loaded = carB.loaded;

destination = carB.destination;

return * this;

}

Several cars coupled together are referred to as a string of cars.

Create another class called StringOfCars, containing:

* a pointer to an array of Car objects in the heap.

* a static const int ARRAY_MAX_SIZE set to 10.

* an int size containing the current number of Cars in the array.

* a default constructor which gets space for the array in the heap,

and sets the the size to zero.

* a copy constructor which gets new space in the heap for the array

and copies all the Car objects.

* a destructor which returns the space to the heap.

* a push function which adds a car to the string of cars.

* a pop function which removes a car from the string of cars, Last In

First Out (LIFO).

* an output function which prints a heading for each car:

car number n where n is the position in the array starting from 1

for the first car and then uses the Car output function to print the

data for each car Or, if the array is empty prints: NO cars

Order of functions in the code:

1. main

2. Car member functions

1. Car constructors in the order

1. default constructor

2. copy constructor

3. other constructors

2. output

3. setUp

4. operator=

3. StringOfCars member functions

1. Car constructors in the order

1. default constructor

2. copy constructor

2. destructor

3. output

4. push

5. pop

4. operator== friend of Car

5. input

Put an eye catcher before the beginning of each function, class, and the

global area:

// class name function name comment(if any)

*************************************************

Modify the main function to do the following tests:

1. Test the Car operator= function.

Before the call to the input function:

Print: TEST 1

Creat a Car object named car1 with initial values:

reportingMark: SP

carNumber: 34567

kind: box

loaded: true

destination: Salt Lake City

Create a Car object named car2 which is a copy of car1

Print car2

2. Test the StringOfCar push function.

Change the input function to have a parameter that is a reference to

a StringOfCars.

Add to main:

Create a default StringOfCars object named string1.

Print: TEST 2

Pass string1 to the input function.

Use the same

cardata.txt

file as in problem 4.1

In the input function, just after creating the car, push it on to

string1.

Remove the print of the car in the input function.

Print: STRING 1

In the main function, after using the input function, print string1.

3. Test the StringOfCars pop function.

Add to main:

Print: TEST 3

Create a car named car3.

Pop one car from string1 into car3.

Print: CAR 3

Print car3.

Print: STRING 1

Then print the contents of string1 again

HERE IS MY CODE FOR 3.3

#include

#include

#include

using namespace std;

// Defining class

class car

{

private:

string reportingMark;

int carNumber;

string kind;

bool loaded;

string destination;

// Constructor for car

public:

car()

{

reportingMark = "";

carNumber = 0;

kind = "other";

loaded = 0;

destination = "NONE";

}

car(const car &object)

{

reportingMark = object.reportingMark;

carNumber = object.carNumber;

kind = object.kind;

loaded = object.loaded;

destination = object.destination;

}

car(string repmark, int num, string k, bool load, string dest)

{

setUp(repmark, num, k, load, dest);

}

~car() { cout << "Killing car function"; }

bool friend operator==(car &c1, car &c2);

// output function

void output()

{

static const int fw = 22;

cout << setw(fw) << "Reporting Mark: " << setw(fw) << reportingMark << endl;

cout << setw(fw) << "Car Number: " << setw(fw) << carNumber << endl;

cout << setw(fw) << "Kind: " << setw(fw) << kind << endl;

if (loaded)

cout << setw(fw) << "Loaded: " << setw(fw) << "true" << endl;

else

cout << setw(fw) << "Loaded: " << setw(fw) << "false" << endl;

cout << setw(fw) << "Destination: " << setw(fw) << destination << endl;

}

// Setting input values to data memnbers

void setUp(string repmark, int num, string k, bool load, string dest)

{

reportingMark = repmark;

carNumber = num;

kind = k;

loaded = load;

destination = dest;

}

};

// input function that prompt user to input values

void input() {}

bool operator==(car &c1, car &c2){

if(c1.reportingMark == c2.reportingMark && c1.carNumber == c2.carNumber)

return true;

return false;

}

int main()

{

string reporting, kind, dst;

int Number;

bool loded;

cout << "enter car reporting mark :";

cin >> reporting;

cout << "enter car number :";

cin >> Number;

cout << "enter car which kind :";

cin >> kind;

cout << "enter car loaded true or false, 1 for true, 0 for false:";

cin >> loded;

// If loaded destination need to be entered

if ((loded == 1))

{

getline(cin, dst);

cout << "enter destination:";

getline(cin, dst);

}

else

dst = "NONE";

// using new creating new object

car car1(reporting, Number, kind, loded, dst);

car car2(car1);

car car3;

cout << "Contents of car 1: ";

car1.output();

cout << "Contents of car 2: ";

car2.output();

cout << "Contents of car 3: ";

car3.output();

if(car1 == car2)

cout<<"car1 is the same car as car2 ";

else

cout<<"car1 is not the same car as car2 ";

if(car2 == car3)

cout<<"car2 is the same car as car3 ";

else

cout<<"car2 is not the same car as car3 ";

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!