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

Problem 4.1 In this problem, we will read cars from a file, rather than typing t

ID: 3919844 • Letter: P

Question

Problem 4.1

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 3.3

* 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 you will be

using the input many times.

?

Use the file named

cardata4.txt

( provided), containing the

following three lines of data:

Car CN 819481 maintenance false NONE

Car SLSF 46871 tank

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

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

code from problem 3.3

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


class car

{
private:

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


public:

car()

{
reportingMark = "NONE";
carNumber = 0;
kind = "NONE";
loaded = false;
destination = "NONE";
}

car(const car &object)

{

reportingMark = object.reportingMark;

carNumber = object.carNumber;

kind = object.kind;

loaded = object.loaded;

destination = object.destination;

}

car(string repmark, int num, string k, bool load, string dest)

{

setUp(repmark, num, k, load, dest);

}

~car() { cout << "Killing car function"; }

bool friend operator==(car &c1, car &c2);

// output function

void output()

{

cout << setw(15) << "Reporting Mark: " << setw(15) << reportingMark << endl;

cout << setw(15) << "Car Number: " << setw(15) << carNumber << endl;

cout << setw(15) << "Kind: " << setw(15) << kind << endl;

if (loaded)

cout << setw(15) << "Loaded: " << setw(15) << "true" << endl;

else

cout << setw(15) << "Loaded: " << setw(15) << "false" << endl;

cout << setw(15) << "Destination: " << setw(15) << destination << endl;

}

// Setting input values to data memnbers

void setUp(string repmark, int num, string k, bool load, string dest)

{

reportingMark = repmark;

carNumber = num;

kind = k;

loaded = load;

destination = dest;

}

};

// input function that prompt user to input values

void input() {}

bool operator==(car &c1, car &c2){

if(c1.reportingMark == c2.reportingMark && c1.carNumber == c2.carNumber)

return true;

return false;

}

int main()

{

string reporting, kind, dst;

int Number;

bool loded;

cout << "enter car reporting mark :";

cin >> reporting;

cout << "enter car number :";

cin >> Number;

cout << "enter car which kind :";

cin >> kind;

cout << "enter car loaded true or false, 1 for true, 0 for false:";

cin >> loded;

if ((loded == 1))

{
getline(cin, dst);
cout << "enter destination:";
getline(cin, dst);}

else
dst = "NONE";

car car1(reporting, Number, kind, loded, dst);
car car2(car1);
car car3;

cout << "Contents of car 1: ";
car1.output();

cout << "Contents of car 2: ";
car2.output();

cout << "Contents of car 3: ";
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;

}

Explanation / Answer

#include <iostream>

#include <string>

#include <iomanip>

#include <fstream>

#include <sstream>

using namespace std;

class car

{

private:

string reportingMark;

int carNumber;

string kind;

bool loaded;

string destination;

public:

car()

{

reportingMark = "NONE";

carNumber = 0;

kind = "NONE";

loaded = false;

destination = "NONE";

}

car(const car &object)

{

reportingMark = object.reportingMark;

carNumber = object.carNumber;

kind = object.kind;

loaded = object.loaded;

destination = object.destination;

}

car(string repmark, int num, string k, bool load, string dest)

{

setUp(repmark, num, k, load, dest);

}

~car()

{

cout << "Killing car function";

}

bool friend operator==(car &c1, car &c2);

// output function

void output()

{

cout << setw(15) << "Reporting Mark: " << setw(15) << reportingMark << endl;

cout << setw(15) << "Car Number: " << setw(15) << carNumber << endl;

cout << setw(15) << "Kind: " << setw(15) << kind << endl;

if (loaded)

cout << setw(15) << "Loaded: " << setw(15) << "true" << endl;

else

cout << setw(15) << "Loaded: " << setw(15) << "false" << endl;

cout << setw(15) << "Destination: " << setw(15) << destination << endl;

}

// Setting input values to data memnbers

void setUp(string repmark, int num, string k, bool load, string dest)

{

reportingMark = repmark;

carNumber = num;

kind = k;

loaded = load;

destination = dest;

}

};

bool operator==(car &c1, car &c2)

{

if(c1.reportingMark == c2.reportingMark && c1.carNumber == c2.carNumber)

return true;

return false;

}

// input function that read data from the file

bool input()

{

string line;

ifstream read;

read.open("cardata4.txt");

if(read.is_open()) {

while(read.peek() != EOF) {

  

getline(read, line);

istringstream iss(line);

  

string type, reporting, kind, loded, dst;

int Number;

if (!(iss >> type >> reporting >> Number >> kind >> loded >> dst)) {

cout << "break";

break;

}

car car1(reporting, Number, kind, (loded == "true" || loded == "TRUE"), dst);

cout << "Contents of car 1: ";

car1.output();

}   

} else {

cout << endl << "Could not open file....";

return false;

}

read.close();

return true;

}

int main()

{

if(!input()) {

return 0;

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote