Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ In this problem, we will read cars from a file, rather than typing them in f

ID: 3861010 • Letter: C

Question

C++

In this problem, we will read cars from a file, rather than typing them

in from the keyboard. Do the steps in order to build the solution to

this problem.

1. Copy and clean up code from problem 3.3.

      * Copy problem C3.

      * Change the name to Assignment 4, Problem 4.1

      * Remove everything from the main function.

      * Remove the execution results.

2. Modify the input function.

      * Remove the & from the parameters in the function header, so they

        are all values rather than references.

      * Move the parameters from the function header and put them within

        the input function, so they are now all local variables.

      * Remove the parameters from the prototype for the input function,

        so it matches the function header.

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

        temp using the constructor that takes the five parameters.

      * Use the Car output function to print the Car.

      * Call the input function from the main function with no arguments.

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

    using the input many times.

      * Create a file containing the following three lines of data (Omit

        the heading line).

        Type   ARR   number   kind          loaded   destination

        Car    CN    819481   maintenance   false    NONE

        Car    SLSF   46871   business      true     Memphis

        Car    AOK      156   tender        true     McAlester

               

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

      * In the input function declare a string named type. Read the Type

        field before reading the ARR field. We do not need the Type

        field yet, but we need to read it to get it out of the way.

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

        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.

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

               

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

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

Order of functions in the code:

1. main

2. Car member functions

     1. constructors in the order

         1. default constructor

         2. copy constructor

         3. other constructors

     2. output

     3. setUp

3. operator== with Car parameters

4. input

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

global area:

// class name function name comment(if any)

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

      Problem 4.2

Copy the solution for problem 4.1

Copy the following operator= overload member function that returns the

left hand operator by reference:

// Car operator= **************************************************

Car & Car::operator=(const Car & carB)

{

reportingMark = carB.reportingMark;

carNumber     = carB.carNumber;

kind          = carB.kind;

loaded        = carB.loaded;

destination   = carB.destination;

return * this;

}

Several cars coupled together are referred to as a string of cars.

Create another class called StringOfCars, containing:

* a pointer to an array of Car objects in the heap.

* a static const int ARRAY_MAX_SIZE set to 10.

* an int size containing the current number of Cars in the array.

* a default constructor which gets space for the array in the heap,

    and sets the the size to zero.

* a copy constructor which gets new space in the heap for the array

    and copies all the Car objects.

* a destructor which returns the space to the heap.

* a push function which adds a car to the string of cars.

* a pop function which removes a car from the string of cars, Last In

    First Out (LIFO).

* an output function which prints a heading for each car:

    car number n where n is the position in the array starting from 1

    for the first car and then uses the Car output function to print the

    data for each car Or, if the array is empty prints: NO cars

Order of functions in the code:

1. main

2. Car member functions

     1. Car constructors in the order

         1. default constructor

         2. copy constructor

         3. other constructors

     2. output

     3. setUp

     4. operator=

3. StringOfCars member functions

     1. Car constructors in the order

         1. default constructor

         2. copy constructor

     2. destructor

     3. output

     4. push

     5. pop

4. operator== friend of Car

5. input

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

global area:

// class name function name comment(if any)

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

Modify the main function to do the following tests:

1. Test the Car operator= function.

    Before the call to the input function:

    Print: TEST 1

    Creat a Car object named car1 with initial values:

      reportingMark: SP

      carNumber: 34567

      kind: box

      loaded: true

      destination: Salt Lake City

    Create a Car object named car2 which is a copy of car1

    Print car2

2. Test the StringOfCar push function.

    Change the input function to have a parameter that is a reference to

    a StringOfCars.

    Add to main:

    Create a default StringOfCars object named string1.

    Print: TEST 2

    Pass string1 to the input function.

    Use the same file as in problem D1.

    In the input function, just after creating the car, push it on to

    string1.

    Remove the print of the car in the input function.

    Print: STRING 1

    In the main function, after using the input function, print string1.

3. Test the StringOfCars pop function.

    Add to main:

    Print: TEST 3

    Create a car named car3.

    Pop one car from string1 into car3.

    Print: CAR 3

    Print car3.

    Print: STRING 1

Then print the contents of string1 again

This is 3.3 code

#include <iostream>
#include <string>

using namespace std;

class Car
{

private:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;


public:
void output();
void setUp(string,int,string,bool,string);

Car()
{
reportingMark = " ";
carNumber = 0;
kind = "others";
loaded = 0;
destination = "none";
}
Car(const Car &car1)
{
reportingMark = car1.reportingMark;
carNumber = car1.carNumber;
kind = car1.kind;
loaded = car1.loaded;
destination = car1.destination;
}
Car(const string &rMark, const int &carNum ,const string &kind1,const bool &load,const string &dest)
{
setUp( rMark, carNum, kind1, load, dest);
}

~Car()
{};
friend bool operator==(const Car &car1,const Car &car2);
};


void input(string &rMark, int &carNum , string &kind1, bool &load, string &dest);

int main()
{

string rMark;
int carNum;
string kind1;
bool load;
string dest;

input(rMark, carNum, kind1, load, dest);

Car car1(rMark, carNum, kind1, load, dest);
Car car2=car1;
Car car3;

car1.setUp(rMark, carNum, kind1, load, dest);

car1.output();
car2.output();
car3.output();

if (car1 == car2)
cout << "car1 is the same car as car2 ";
else
cout << "car1 is not the same car as car2 ";

if (car2 == car3)
cout << "car2 is the same car as car3 ";
else
cout << "car2 is not the same car as car3 ";


return 0;
}

void Car::output()
{
cout<<"The data is"<<endl;
cout<<"Reporting Mark: "<< reportingMark <<endl;
cout<<"Car Number: "<< carNumber <<endl;
cout<<"Car Kind: "<< kind <<endl;
cout<<"Car loaded: "<< boolalpha << loaded <<endl;
cout<<"Car Destination: "<< destination <<endl;
cout<<endl;
}

void Car::setUp(string rMark, int carNum , string kind1, bool load, string dest)
{
reportingMark=rMark;
carNumber=carNum;
kind=kind1;
loaded=load;
destination=dest;
}

bool operator ==(const Car &car1, const Car &car2)
{
if(car1.reportingMark == car2.reportingMark && car1.carNumber== car2.carNumber)
return true;

else
return false;
}
void input(string &rMark, int &carNum , string &kind1, bool &load, string &dest)
{
cout<<"Enter the car mark: ";
cin>>rMark;

cout<<"Enter the car number: ";
cin>>carNum;

cout<<"Enter the kind of the car: ";
cin>>kind1;

cout<<"Enter the value for load: ";
cin>>boolalpha >> load;

cout<<"Enter your destination: ";
cin.ignore();
getline(cin,dest);


cout<<endl;
}

Plz do not use std::

Explanation / Answer

#include <iostream>
#include <string>
#include <fstream> // std::ifstream
#include <iomanip>
using namespace std;
/* ********* Class Car *************
********************************* */
class Car
{
private:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public:
Car()
{
reportingMark = "";
carNumber = 0;
kind = "Others";
loaded = 0;
destination = "NONE";
}
//copy constructor
Car(Car &car)
{
reportingMark = car.reportingMark;
carNumber = car.carNumber;
kind = car.kind;
loaded = car.loaded;
destination = car.destination;
}
~Car()
{
}


/* ************* Function output ************
show all results of the program
******************************************* */
void output()
{
string strLoaded;
if (loaded==true)
{
strLoaded = "TRUE";
}
else
{
strLoaded = "FALSE";
}
cout << fixed << left << setw(18) << "Reporting Mark" << reportingMark << endl;
cout << fixed << left << setw(18) << "Car Number" << carNumber << endl;
cout << fixed << left << setw(18) << "Kind" << kind << endl;
cout << fixed << left << setw(18) << "Loaded" << strLoaded << endl;
cout << fixed << left << setw(18) << "Destination" << destination << endl;
cout << endl;
}

void setUp(string &r, int &c , string &k, bool &l, string &d)
{
reportingMark =r;
carNumber = c;
kind = k;
loaded = l;
destination = d;
}

friend bool operator== (const Car &c1, const Car &c2);
};

bool operator== (const Car &c1, const Car &c2)
{
return (c1.reportingMark== c2.reportingMark &&
c1.carNumber == c2.carNumber );
}

void input();

//*********** Main **************
int main()
{
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
input ();

Car car1;
car1.setUp(reportingMark,carNumber, kind, loaded, destination);
cout<<"Contents of car1:"<<endl;
car1.output();

//copy constructor
Car car2=car1;
//call output funciton
cout<<"Contents of car1:"<<endl;
car2.output();
//default constructor
Car car3;
cout<<"Contents of car1:"<<endl;
//call output funciton
car3.output();

if (car1 == car2)
cout<< "car1 is the same car as car2 ";
else
cout<< "car1 is not the same car as car2 ";
if (car2 == car3)
cout<< "car2 is the same car as car3 ";
else
cout<< "car2 is not the same car as car3 ";

return 0;
}
/* ************ Function Input **************
Collect the data
****************************************** */
void input()
{
string type;
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
  
std::ifstream inputFile ("input.txt");

if (inputFile.is_open())
{
while(inputFile.peek() != EOF)
{
inputFile >> type >> reportingMark >> carNumber >> kind >> loaded >> destination;
Car temp(string reportingMark, int carNumber, string kind, bool loaded);
}
}
  
else
{
// show message:
std::cout << "Error opening file";
return ;
}

inputFile.close();

/*
int len;
string choice;
cout<< "Enter reportingMark with 5 or less upper case characters: ";
getline(cin, reportingMark);
len= reportingMark.length();
while (len > 5)
{
cout<< "invalid! Enter reportingMark with 5 or less upper case characters: ";
getline(cin, reportingMark);
len= reportingMark.length();
}
cout<< "Enter Car Number: ";
cin>> carNumber;
cout << "Enter kind of car: ";
cin>> kind;
do
{
cout<<"Enter the loaded only true or false:";
cin>>choice;
if (choice == "true")
{
loaded = true;
}
else if (choice == "false")
{
loaded = false;
}
else
{
cout<<"error!"<<endl;
}
}
while (choice != "true" && choice != "false");

if(loaded==true)
{
cout<<"Entre the destination:";
cin.ignore();
getline(cin,destination);
}
if(loaded ==false)
{
destination = "NONE";
}

Car temp(string reportingMark, int carNumber, string kind, bool loaded);
*/

}