Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the code from Lab 4 . 1 , add the ability to read from a file. Modify the input function: * Use the Cargo

Using the code from Lab 4.1, add the ability to read from a file.
Modify the input function:
* Use the Cargo output function to print the Cargo object.
* At the bottom of the input function, create a Cargo object named temp using the
constructor that takes six parameters.
* 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. You will be using the input file many times.
* Create a file named cardata4.txt
(Photo is attatched)
* All weights are in pounds, dont worry about kilograms. You may
comment out or remove that code since user interaction with the
program will be limited to file name input.
* In the input function, declare an object of type ifstream named
inputFile, which we will use to read from the file. Ask the user
for the name of the input file, and store it in a C++ string.
* 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. We will extend error checking in lab 5.
* 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.
* 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 as the building and output of the unit.
Hint: you can do this with the following while statements
while(getline(inputFile, cargostring))
{
istringstream cargoISS(cargostring);
cargoISS >> type1;
if ((type1.compare(comp1))==0)
{
cargoISS >> type2>> abrv >> uld >> plane >> weight >> dest;
type1= type1+""+ type2;
if (!((type2.compare(comp2))==0))
{
cout type1" bad unit name
";
exit (99);
}
}
else
{
cargoISS >> abrv >> uld >> plane >> weight >> dest;
}
This code uses input stream processing and parsing to simplify data reading from a
file.
Put an eye catcher before the beginning of each function, class, and the global area:
// class name function name comment(if any)
CODE FROM LAB 4.1
#include
#include
using namespace std;
class Cargo {
private:
string uldtype;
string abbrev;
string uldid;
int aircraft;
double weight;
string destination;
public:
// Constructors
Cargo() : aircraft(0), weight(0.0){}
Cargo(const string& uldtype, const string& abbrev, const string& uldid, int aircraft, double weight, const string& destination)
: uldtype(uldtype), abbrev(abbrev), uldid(uldid), aircraft(aircraft), weight(weight), destination(destination){}
// Destructor
~Cargo(){
cout "Cargo destructor called" endl;
}
// Setters
void setuldtype(const string& uldtype){ this->uldtype = uldtype; }
void setabbrev(const string& abbrev){ this->abbrev = abbrev; }
void setuldid(const string& uldid){ this->uldid = uldid; }
void setaircraft(int aircraft){ this->aircraft = aircraft; }
void setweight(double weight){ this->weight = weight; }
void setdestination(const string& destination){ this->destination = destination; }
// Getters
string getuldtype() const { return uldtype; }
string getabbrev() const { return abbrev; }
string getuldid() const { return uldid; }
int getaircraft() const { return aircraft; }
double getweight() const { return weight; }
string getdestination() const { return destination; }
// Overloaded operator ==
friend bool operator==(const Cargo& c1, const Cargo& c2){
return (c1.abbrev == c2.abbrev) && (c1.uldid == c2.uldid);
}
};
// Function prototypes
void inputCargo(Cargo& cargo);
void outputCargo(const Cargo& cargo);
// Input
void inputCargo(Cargo& cargo){
cout "Enter unit load type: ";
cin >> cargo.uldtype;
cout "Enter unit load abbreviation: ";
cin >> cargo.abbrev;
cout "Enter unit identifier: ";
cin >> cargo.uldid;
cout "Enter aircraft type: ";
cin >> cargo.aircraft;
cout "Enter unit weight (in pounds): ";
cin >> cargo.weight;
cout "Enter destination code: ";
cin >> cargo.destination;
}
// Output
void outputCargo(const Cargo& cargo){
cout "Unit load type: " cargo.getuldtype() endl;
cout "Unit load abbreviation: " cargo.getabbrev() endl;
cout "Unit identifier: " cargo.getuldid() endl;
cout "Aircraft type: " cargo.getaircraft() endl;
cout "Unit weight: " cargo.getweight()" pounds" endl;
cout "Destination code: " cargo.getdestination() endl;
}
*The rest of the code is attatched*
image text in transcribed

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

Recommended Textbook for

More Books

Students also viewed these Databases questions