Question: Lab 4.2 Using the code from lab 4.1, add the ability to read from a file. Modify the input function: * Move the input function

Lab 4.2

Using the code from lab 4.1, add the ability to read from a file.

Modify the input function:

* Move the input function out of the Cargo class to just below the end of the Cargo class

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

temp using the constructor that takes the six parameters.

* Use the Cargo output function to print the Cargo object.

* Call the input function from the main function with no arguments. Remove the rest of the code

from main

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

using the input many times.

* Create a file named cardata4.txt (or use the one provided),

containing the following three lines of data.

If you create your own, make sure to follow the format

below:

Pallet KMG KMG45982IB 737 4978 SEA

Container SEA SEA12345SF 777 2468 JFK

Container SFO SFO67890TY 747 3579 TYO

* All weights are in pounds, dont worry about kilograms. You may

comment out that code.

* 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.

* Hint: We need to use getline when reading the strings.

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. Your output will then be much neater.

* 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) or use this form while(inputFile >> type)

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

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

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

global area:

// class name function name comment(if any)

Pallet KMG KMG45982IB 737 4978 SEA

Container SEA SEA12345SF 737 2468 JFK

Container SFO SFO67890TY 737 3579 TYO

My lab4.1 code (Feel free to change this if needed):

#include "C++ 22B lab4.1.hpp"

#include

using namespace std;

class Unit {

string uld;

string abbreviation;

string uldid;

int aircraft;

int weight;

string destination;

public:

Unit() {

this->uld = "default uld";

this->abbreviation = "default abbreviation";

this->uldid = "default uldid";

this->aircraft = 0;

this->destination = "default destination";

}

Unit(string uld, string abbreviation, string uldid, int aircraft, int weight, string destination) {

this->uld = uld;

this->abbreviation = abbreviation;

this->uldid = uldid;

this->aircraft = aircraft;

this->destination = destination;

}

Unit(const Unit &p1) {

this->uld = p1.uld;

this->abbreviation = p1.abbreviation;

this->uldid = p1.uldid;

this->aircraft = p1.aircraft;

this->destination = p1.destination;

}

bool operator==(Unit const &obj) {

return this->uld == obj.uld && this->abbreviation == obj.abbreviation && this->uldid == obj.uldid &&

this->aircraft == obj.aircraft &&

this->destination == obj.destination;

}

};

int main() {

Unit unit1("Pallet", "KMG", "KMG32597IB", 737, 3321, "SEA");

Unit unit2 = unit1;

Unit unit3;

if (unit1 == unit2)

cout << " unit1 is the same as unit2 ";

else

cout << " unit1 is not the same as unit2 ";

if (unit2 == unit3)

cout << " unit2 is the same as unit3 ";

else

cout << " unit2 is not the same as unit3 ";

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!