Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / I need help with this C++ lab

I need help with this C++ lab

Computer Science

I need help with this C++ lab. Thank you.

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

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

     using the input many times.

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

containing the following three lines of data.

If you make 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, don't worry about kilograms. You may

        comment out that code.

     * In the input function, declare an object of 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. Use a

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

       well building and output of the Car.

       Hint: 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)

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

HERE IS THE LAB CODE 4.1

#include <iostream>

#include <string>

#include <cstdlib>

#define MAX 100

using namespace std;

// Defines a class to store cargo information

class Cargo

{

string uld;

string abbreviation;

string uldid;

int aircraft;

int weight;

string destination;

// Function to return true if the parameter no is within second parameter min and third parameter max

// otherwise returns false

bool valid(int no, int min, int max)

{

// Checks if no is greater than or equals to min and less than or equals to max return true

if(no >= min && no <= max)

return true;

// Otherwise returns false

else

return false;

}// End of function

public:

// default constructor, does nothing

Cargo(){

}

// Copy constructor

Cargo(const Cargo &c2) {

uld = c2.uld;

abbreviation = c2.abbreviation;

uldid = c2.uldid;

aircraft = c2.aircraft;

weight = c2.weight;

destination = c2.destination;

}

// friend function, kilotopound, which will convert kilograms to pounds

friend int kilotopound(Cargo c)

{

// return pounds

return (c.weight * 2.2);

}

// Function to accept cargo information from the user

void input()

{

int type, abb;

// Loops till valid unit load entered by the user

do

{

// Displays menu

cout<<"n 1 - Container t 2 - Pallet";

// Accepts unit load

cout<<"n Enter the Unit Load: ";

cin>>type;

// Calls the function to validate input type

if(valid(type, 1, 2))

{

// Checks the type and assigns appropriate constant

if(type == 1)

uld = "Container";

else

uld = "Pallet";

// Come out of the loop

break;

}// End of if condition

// Otherwise invalid data displays error message and continue

else

cout<<"n ERROR: Invalid type Unit Load. Try again.";

}while(true);// End of do - while loop

// Loops till valid container type entered by the user

do

{

// Checks if type is 1 then Container

if(type == 1)

{

// Displays menu

cout<<"n Container type: t 1 - AYF t 2 - AYK t 3 - ABB t 4 - AYY ";

// Accepts abbreviation

cout<<"n Enter the Abbreviation: ";

cin>>abb;

// Calls the function to validate input type

if(valid(abb, 1, 4))

{

// Checks the abb and assigns appropriate constant

if(abb == 1)

abbreviation = "AYF";

else if(abb == 2)

abbreviation = "AYK";

else if(abb == 3)

abbreviation = "ABB";

else if(abb == 4)

abbreviation = "AYY";

// Come out of the loop

break;

}// End of inner if condition

// Otherwise invalid data displays error message and continue

else

cout<<"n ERROR: Invalid type Abbreviation. Try again.";

}// End of if outer condition

// Checks if type is 2 then Pallets

else if(type == 2)

{

// Displays menu

cout<<"n Pallets type: t 1 - PAG t 2 - PMC t 3 - PLA ";

cout<<"n Enter the Abbreviation: ";

cin>>abb;

// Calls the function to validate input type

if(valid(abb, 1, 3))

{

// Checks the abb and assigns appropriate constant

if(abb == 1)

abbreviation = "PAG";

else if(abb == 2)

abbreviation = "PMC";

else if(abb == 3)

abbreviation = "PLA";

// Come out of the loop

break;

}// End of if condition

// Otherwise invalid data displays error message and continue

else

cout<<"n ERROR: Invalid type Abbreviation. Try again.";

}// End of else if condition

}while(true);// End of do - while loop

 

// Loops till valid unit id entered by the user

do

{

string uid;

// Accepts uid

cout<<"n Enter the Unit ID (5 digit): ";

cin>>uid;

// Checks if length of the uid is 5 then valid

if(uid.length() == 5)

{

// Checks if type is 1 then Container

if(type == 1)

{

// Checks the abb and concatenate constant with uid accordingly

if(abb == 1)

uldid = "AYF" + uid + "IB";

else if(abb == 2)

uldid = "AYK" + uid + "IB";

else if(abb == 3)

uldid = "ABB" + uid + "IB";

else

uldid = "AYY" + uid + "IB";

}// End of inner if condition

// Otherwise type is 2 for Pallets

else

{

// Checks the abb and concatenate constant with uid accordingly

if(abb == 1)

uldid = "PAG" + uid + "IB";

else if(abb == 2)

uldid = "PMC" + uid + "IB";

else

uldid = "PLA" + uid + "IB";

}// End of else

// Come out of the loop

break;

}// End of outer if condition

// Otherwise invalid data displays error message and continue

else

cout<<"n ERROR: Invalid Unit ID. Try again.";

}while(true);// End of do - while loop

cout<<"n Enter Aircraft type: ";

cin>>aircraft;

cout<<"n Enter Weight: ";

cin>>weight;

// declare a variable to store userChoice of weight

int userWeightChoice;

// Prompting the user to select the unit of weight

cout <<"nEnter 1 if weight is in pounds or 2 if weight is in kilograms: " <<endl; // getting user userWeightChoice

cin >> userWeightChoice;

// if the weight choice selected by user is kilogram

if(userWeightChoice == 1){

// call friend function kilotopound

weight = kilotopound(*this);

}

// Loops till valid destination entered by the user

do

{

// Accepts destination

cout<<"n Enter Destination (3 characters): ";

cin>>destination;

// Checks if length of the destination is 3 then valid

if(destination.length() == 3)

// Come out of the loop

break;

// Otherwise invalid data displays error message and continue

else

cout<<"n ERROR: Invalid Destination. Try again.";

}while(true);// End of do - while loop

}// End of function

// Function to display cargo information

void output()

{

cout<<"n ******************";

cout<<"nt Unit Load: "<<uld;

cout<<"nt Abbreviation: "<<abbreviation;

cout<<"nt Unit ID: "<<uldid;

cout<<"nt Aircraft Type: "<<aircraft;

cout<<"nt Weight: "<<weight<<" pounds";

cout<<"nt Destination: "<<destination;

cout<<"n ******************";

}// End of function

// function that overloads == operator that returns true if abbreviation and uldid are same else return false

bool operator==(const Cargo &other) const

{

return((abbreviation == other.abbreviation) && (uldid == other.uldid));

}

};// End of structure

int main()

{

// create unit1 using default constructor

Cargo unit1;

// input the values for unit1 object

cout<<"Enter values for unit1: "<<endl;

unit1.input();

// make a copy of unit1 using a copy constructor

Cargo unit2(unit1);

Cargo unit3; // create an object using default constructor

// check if unit1 and unit2 are equal

if(unit1 == unit2)

cout<<"n unit1 is the same as unit2n";

else

cout<<"n unit1 is not the same as unit2n";

// check if unit2 and unit3 are equal

if(unit2 == unit3)

cout<<"n unit2 is the same as unit3n";

else

cout<<"n unit2 is not the same as unit3n";

/*

// Create object1

cout << "Enter values for object1" << endl;

Cargo object1;

object1.input();

// create ibject 2 using copy constructor

Cargo object2 = object1;

//print both

cout << "Object1" << endl;

object1.output();

cout << endl << "Object2 creating using copy constructor" << endl;

object2.output();

 

// Creates an array object of class cargo

Cargo cargo[MAX];

// To store number of records

int counter = 0;

// To store user choice

int choice;

// Loops till user choice is not 3

do

{

// Displays menu

cout<<"n ******** MENU **********";

cout<<"nt 1 - Input Cargo Information nt 2 - Display Cargo Information nt 3 - Exit";

// Accepts user choice

cout<<"ntt What is your choice? ";

cin>>choice;

// Checks user choice and calls appropriate function

switch(choice)

{

case 1:

// Checks if current record counter is equals to MAX then display error message

// list full

if(counter == MAX)

cout<<"n ERROR: Full List. Can not add.";

else

{

// Calls the function to accept cargo data

cargo[counter++].input();

}// End of else

break;

case 2:

// Checks if current record counter is equals to 0 then display error message

// empty list

if(counter == 0)

cout<<"n ERROR: Nothing to display. Empty list.";

else

{

for(int c = 0; c < counter; c++)

// Calls the function to display data

cargo[c].output();

}// End of else

break;

case 3:

cout<<"ntt Thanks.";

exit(0);

default:

cout<<"n Invalid choice!!";

}// End of switch - case

}while(true);// End of do - while loop

*/

 

return 0;

} // End of main function

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE