Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ Using the code from lab 4.1, add the ability to read from a file. Modify the input function: * Move the input function out

c++

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 PAG PAG45982IB 737 4978 OAK

Container AYF AYF23409AA 737 2209 LAS

Container AAA AAA89023DL 737 5932 DFW

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

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

--------------------this is my code for 4.1---------------------------------

#include #include #include #include

using namespace std;

class Cargo { private: string uldtype; string abbrev; string uldid; int aircraft; int weight; string destination; public:

///Default constructor prototype Cargo();

///full constructor protype Cargo(const string uldtype, const string abbrev, const string uldid, const int aircraft, const int weight, const string destination);

///Copy constructor Cargo(const Cargo &unit);

///Destructor prototype ~Cargo();

void setuldtype(string); void setabbrev(string); void setuldid(string); void setaircraft(int); void setweight(double); void setdestination(string);

/// Accessor (getters) prototypes string getuldtype() const; string getabbrev() const; string getuldid() const; int getaircraft() const; double getweight() const; string getdestination() const;

void input(); void output(); friend double kilotolb(double wtin); friend bool operator == (const Cargo &left, const Cargo &right); ///overload below };

double kilotolb(double &);

int main() { Cargo unit1; unit1.input(); Cargo unit2(unit1); Cargo unit3; cout << endl; cout << "---Unit1--- "; unit1.output(); cout << endl; cout << "---Unit2--- "; unit2.output(); cout << endl; cout << "---Unit3--- "; unit3.output(); 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; } Cargo::Cargo() { uldtype = "xxx"; abbrev = "00000"; uldid = "DEFAULT"; aircraft = 700; weight = 0.0; destination = "NONE"; } Cargo::Cargo(const string type, const string abrv, const string id, const int craft, const int wt, const string dest) { uldtype = type; abbrev = abrv; uldid = id; aircraft = craft; weight = wt; destination = dest; }

Cargo::Cargo(const Cargo &unit1) { uldtype = unit1.uldtype; abbrev = unit1.abbrev; uldid = unit1.uldid; aircraft = unit1.aircraft; weight = unit1.weight; destination=unit1.destination; }

Cargo::~Cargo() { cout << endl; cout << "NOTE: Cargo destructor called" << endl; } void Cargo::setuldtype(string type) { uldtype = type; } void Cargo::setabbrev(string abrv) { abbrev = abrv; } void Cargo::setuldid(string id) { uldid = id; } void Cargo::setaircraft(int air) { aircraft= air; } void Cargo::setweight(double wt) { char ans; cout << "is weight in kilos(K) or pounds(P)? "; cin >> ans; if(ans == 'k' || ans == 'K') wt = kilotolb(wt); weight = wt; } void Cargo::setdestination(string dest) { destination = dest; } string Cargo::getuldtype()const { return uldtype; } string Cargo::getabbrev() const { return abbrev; } string Cargo::getuldid() const { return uldid; } int Cargo::getaircraft() const { return aircraft; } double Cargo::getweight() const { return weight; } string Cargo::getdestination() const { return destination; } void Cargo::output() { cout << fixed << showpoint << setprecision(2); cout << setw(19) << "The unit load type: " << uldtype << endl; cout << setw(19) << "The unit abbreviation: " << abbrev << endl; cout << setw(19) << "The unit identifier: " << uldid << endl; cout << setw(19) << "The Aircraft type:" << aircraft << endl; cout << setw(19) << "The unit weight:"<< weight << "pounds" << endl; cout << setw(19) << "The Destination code: " << destination << endl; } void Cargo::input() { string type; string abrv; string id; int air; int wt; string dest;

cout << " Please input load information "; cout << left << "Container or Pallet? "; cin >> type; setuldtype (type); cout << " Unit abbreviation? "; cin >> abrv; setabbrev(abrv); cout << " Unit id? "; cin >> id; setuldid(id); cout << " Aircraft type "; cin >> air; setaircraft(air); cout << " Loaded weight? "; cin >> wt; setweight(wt); cout << " Destination? "; cin >> dest; setdestination(dest); cout << endl; } double kilotolb(double& wtin) { wtin = wtin* 2.2; return wtin; } bool operator == (const Cargo &right, const Cargo &left) { //if (strcmp (left.destination, right.destination)) //cout << "destinations are the same "; return (left.abbrev == right.abbrev && left.uldid == right.uldid && left.destination == right.destination); }

----------------------- this is my current code for the input function for 4.2 can u show me how to do it as well as the full code for 4.2-------------------------------- void Cargo::input() { string type; string abrv; string id; int air; int wt; string dest;

ifstream inputFile; inputFile.open ("cardata4.txt") if(!inputFile) { std::cerr << "Error opening the file" << endl; exist(99);

} while (inputFile.peek() != EOF) inputFile.get(); inputFile >> abrv; inputFile >> id; inputFile >> air; inputFile >> wt; inputFile >> dest; Cargo temp(type, abrv, id, air, wt, dest); temp.output(); } inputFile.close();

} double kilotolb(double& wtin) { wtin = wtin* 2.2; return wtin; } bool operator == (const Cargo &right, const Cargo &left) { //if (strcmp (left.destination, right.destination)) //cout << "destinations are the same "; return (left.abbrev == right.abbrev && left.uldid == right.uldid && left.destination == right.destination); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

explain the need for human resource strategies in organisations

Answered: 1 week ago

Question

describe the stages involved in human resource planning

Answered: 1 week ago