Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ this is my code and i use NULL instead of nullptr because the program says that nullptr is not declared in this scope. but

C++

this is my code and i use NULL instead of nullptr because the program says that nullptr is not declared in this scope. but it didn't give a result that i want plz help !!!!

#include #include #include #include #include #include using namespace std;

enum Kind { business, maintenance, other, box, tank, flat, otherFreight, chair, sleeper, otherPassenger }; const string KIND_ARRAY[] = { "business", "maintenance", "other", "box", "tank", "flat", "otherFreight", "chair", "sleeper", "otherPassenger" };

class Car { protected: string reportingMark; int carNumber; Kind kind; bool loaded; public: string destination;

void setUp(string rMark, int carNum, string carKind, bool isLoaded, string dest); virtual void setKind(const string & tempKind); void output(); Car& operator=(const Car & carB); friend bool operator==(const Car &carA, const Car &carB);

Car() { setUp("", 0, "other", false, "NONE"); } Car(const Car &oldCar) { setUp(oldCar.reportingMark, oldCar.carNumber, KIND_ARRAY[oldCar.kind], oldCar.loaded, oldCar.destination); kind = oldCar.kind; } Car(const string &reportingMark, const int &carNumber, const string &kind, const bool &loaded, const string &destination) { setUp(reportingMark, carNumber, kind, loaded, destination); } int getCarNum(){ return carNumber; } string getRepMark(){ return reportingMark; }

virtual ~Car() {};

};

class FreightCar : public Car { public: FreightCar() { setUp("", 0, "other", false, "NONE"); } FreightCar(const FreightCar &oldFreight) { setUp(oldFreight.reportingMark, oldFreight.carNumber, KIND_ARRAY[oldFreight.kind], oldFreight.loaded, oldFreight.destination); } FreightCar(const string &reportingMark, const int &carNumber, const string &kind, const bool &loaded, const string &destination) { setUp(reportingMark, carNumber, kind, loaded, destination); } void setKind(const string & stringKind); };

class PassengerCar : public Car { public: PassengerCar() { setUp("", 0, "other", false, "NONE"); } PassengerCar(const PassengerCar &oldPassenger) { setUp(oldPassenger.reportingMark, oldPassenger.carNumber, KIND_ARRAY[oldPassenger.kind], oldPassenger.loaded, oldPassenger.destination); } PassengerCar(const string &reportingMark, const int &carNumber, const string &kind, const bool &loaded, const string &destination) { setUp(reportingMark, carNumber, kind, loaded, destination); } void setKind(const string & stringKind);

};

//StringOfCars declaration for the Node class class StringOfCars;

class Node { private: Node * next; Car * data; Node() { next = NULL; data = NULL; } public: friend class StringOfCars;

};

class StringOfCars { private: Node * head; Node * tail;

public: StringOfCars() { head = NULL; tail = NULL; }

StringOfCars(const StringOfCars & oldStringOfCars) { Node * currentNodePtr = oldStringOfCars.head; head = NULL; tail = NULL;

if (oldStringOfCars.head != NULL) { while (currentNodePtr != NULL) { push(*currentNodePtr->data); currentNodePtr = (*currentNodePtr).next; } } }

~StringOfCars() {}

void push(const Car & tempCar); Car* search(int carNum); void addCar(); void output(); };

//Function Prototypes void input(StringOfCars & carArray); void buildCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray); void buildFreightCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray); void buildPassengerCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray);

int main() { StringOfCars stringOfCars1; input(stringOfCars1);

StringOfCars stringOfCars2(stringOfCars1); stringOfCars2.output();

cout << "Enter the car number you'd like to search: "; int carNumChoice; cin >> carNumChoice; cout << " Search results: ";

if(stringOfCars2.search(carNumChoice) != NULL){ (stringOfCars2.search(carNumChoice))->output(); }else{ cout << "No car found. "; }

int closeWindow; cout << " Enter an integer to close the output window: "; cin >> closeWindow; }

// ********************************************************** // Car Member Functions // **********************************************************

/*************************** Car::output ***************************** Outputs the member data in a neat format Output is a member function of the Car class */ void Car::output() { cout << "Reporting Mark: " << reportingMark << endl; cout << "Car Number : " << carNumber << endl; cout << "Kind : " << KIND_ARRAY[kind] << endl;

//Converts the loaded boolean into a string for output string temp; if (loaded == true) temp = "true"; else temp = "false";

cout << "Loaded : " << temp << endl; cout << "Destination : " << destination << endl; cout << endl;

}

/*************************** Car::setUp ***************************** Assigns the data to the member data in an object from the Car class setUp is a member function of the Car class */ void Car::setUp(string rMark, int carNum, string carKind, bool isLoaded, string dest) { reportingMark = rMark; carNumber = carNum; setKind(carKind); loaded = isLoaded; destination = dest; }

/*********************** Kind Car::setKind *************************** Sets the kind variable for the setUp function */ void Car::setKind(const string & stringKind) { if (stringKind == "business") kind = business; else if (stringKind == "maintenance") kind = maintenance; else kind = other; }

/************************* Car::operator= ***************************** Returns the left hand operator by refrence operator= is an overloaded member function from the Car class */ Car & Car::operator=(const Car & carB) { setUp(carB.reportingMark, carB.carNumber, KIND_ARRAY[carB.kind], carB.loaded, carB.destination);

return *this; }

/***************************** operator== ************************************** Compares two Car objects to check equivalence (same reportingMark and carNumber operator== is a overloaded friend function from the Car class */ bool operator==(const Car &carA, const Car &carB) { bool temp; if (carA.reportingMark == carB.reportingMark && carA.carNumber == carB.carNumber) temp = true; else temp = false; return temp; }

// ********************************************************** // FreightCar Member Functions // ********************************************************** void FreightCar::setKind(const string & stringKind) { if (stringKind == "box") kind = box; else if (stringKind == "tank") kind = tank; else if (stringKind == "flat") kind = flat; else kind = otherFreight; } // ********************************************************** // PassengerCar Member Functions // ********************************************************** void PassengerCar::setKind(const string & stringKind) { if (stringKind == "chair") kind = chair; else if (stringKind == "sleeper") kind = sleeper; else kind = otherPassenger; }

// ********************************************************** // StringOfCars Member Functions // **********************************************************

/*********************** StringOfCars::addCar *************************** This was added by Jose Sepulveda on 9/27/2015 This function adds a car from the list */

void StringOfCars::addCar(){ string type, order, rMark; cout << "Would you like to add a: 1)Car 2)PassengerCar 3)FrieghtCar 4)Exit "; int choice; cin >> choice; while(choice > 4){ cout << "Sorry, please enter a valid input. "; cin >> choice; } if(choice == 1){ type = "Car"; } else if(choice == 2){ type = "PassengerCar"; } else { type = "FreightCar"; } order = "car11";

cout << "Please enter the rMark: "; cin >> rMark;

cout << "Please enter the car number: "; int carNum; cin >> carNum;

cout << "In one word, what kind of car is it?: "; string carKind; cin >> carKind;

bool isLoaded; isLoaded = true;

cout << "In one word, where is it headed?: "; string dest; cin >> dest;

Car temp(rMark, carNum, carKind, isLoaded, dest); push(temp);

}

/*********************** StringOfCars::search *************************** This function was added by Jose Sepulveda(10/1/2015) Searchs for a car using the carNum, returns ptr to found car, or nullptr if no car is found. */ Car* StringOfCars::search(int carNum){ Node * pCurr; if (head == NULL){ cout << "No cars "; }else{ pCurr = head; while(pCurr != NULL){ if(carNum == (*pCurr->data).getCarNum()){ return &(*pCurr->data); } pCurr = (*pCurr).next; } } Car* temp = NULL; return temp;

}

/*********************** StringOfCars::output *************************** Outputs the data from the linked list of pointers to car objects Prints the data saved in each car object */ void StringOfCars::output() { Node * currentNodePtr; if (head == NULL) { cout << "No cars "; } else { currentNodePtr = head; int currentCarNumber = 0; while (currentNodePtr != NULL) { cout << "Car Number " << ++currentCarNumber << endl; (*currentNodePtr->data).output(); currentNodePtr = (*currentNodePtr).next; } } }

/*********************** StringOfCars::push *************************** Adds a car pointer to the list of pointers to cars Car object accessed through constant refrence from calling function */ void StringOfCars::push(const Car & tempCar) { Car* currentCarPtr = new Car(tempCar); Node* currentNodePtr = new Node;

(*currentNodePtr).data = currentCarPtr;

if (head == NULL) { head = currentNodePtr; tail = currentNodePtr; } else { (*tail).next = currentNodePtr; tail = currentNodePtr; }

}

/****************************** input ******************************** Reads in information on the train car from a file Information saved in temporary car object in function */ void input(StringOfCars & carArray) { string type; string order; string rMark; int carNum; string carKind; bool isLoaded; string dest; ifstream inputFile;

inputFile.open("cardata51.txt");

if(inputFile.is_open()== false) { cerr<<"Error!! Can't read file... "; exit(0); }

while (inputFile.peek() != EOF) { inputFile >> type; inputFile >> order; inputFile >> rMark; inputFile >> carNum; inputFile >> carKind;

//Converts the user inputted true/false string to a boolean value string temp; inputFile >> temp; if (temp == "true") isLoaded = true; else if (temp == "false") isLoaded = false;

//Skips the white space while (inputFile.peek() == ' ') inputFile.get();

getline(inputFile, dest);

if (type == "Car") buildCar(rMark, carNum, carKind, isLoaded, dest, carArray); else if (type == "FreightCar") buildFreightCar(rMark, carNum, carKind, isLoaded, dest, carArray); else if (type == "PassengerCar") buildPassengerCar(rMark, carNum, carKind, isLoaded, dest, carArray); }

inputFile.close(); }

/*********************** Car buildCar *************************** Builds a car object and outputs the results */ void buildCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray) { Car obj = Car(rMark, carNum, carKind, isLoaded, dest); carArray.push(obj); }

/*********************** Car buildFreightCar *************************** Builds a freight car object and outputs the results */ void buildFreightCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray) { FreightCar obj = FreightCar(rMark, carNum, carKind, isLoaded, dest); carArray.push(obj); }

/*********************** Car buildPassengerCar *************************** Builds a passenger car object and outputs the results */ void buildPassengerCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray) { PassengerCar obj = PassengerCar(rMark, carNum, carKind, isLoaded, dest); carArray.push(obj); }

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

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions