Answered step by step
Verified Expert Solution
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 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.
Create a file and use it for input. You will be using the input file many times.
Create a file named cardatatxt
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
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
whilegetlineinputFile cargostring
istringstream cargoISScargostring;
cargoISS type;
if typecomparecomp
cargoISS type abrv uld plane weight dest;
type type type;
if typecomparecomp
cout type bad unit name
;
exit ;
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 commentif any
CODE FROM LAB
#include
#include
using namespace std;
class Cargo
private:
string uldtype;
string abbrev;
string uldid;
int aircraft;
double weight;
string destination;
public:
Constructors
Cargo : aircraft weight
Cargoconst string& uldtype, const string& abbrev, const string& uldid, int aircraft, double weight, const string& destination
: uldtypeuldtype abbrevabbrev uldiduldid aircraftaircraft weightweight destinationdestination
Destructor
~Cargo
cout "Cargo destructor called" endl;
Setters
void setuldtypeconst string& uldtype thisuldtype uldtype;
void setabbrevconst string& abbrev thisabbrev abbrev;
void setuldidconst string& uldid thisuldid uldid;
void setaircraftint aircraft thisaircraft aircraft;
void setweightdouble weight thisweight weight;
void setdestinationconst string& destination thisdestination 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 operatorconst Cargo& c const Cargo& c
return cabbrev cabbrev && culdid culdid;
;
Function prototypes
void inputCargoCargo& cargo;
void outputCargoconst Cargo& cargo;
Input
void inputCargoCargo& 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 outputCargoconst 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started