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

This code is to simulate a train where you start with an engine and you can add

ID: 3778229 • Letter: T

Question

This code is to simulate a train where you start with an engine and you can add train cars, detach train cars and move between train cars. I have no problem with that but I have a problem with deleting all my new boxes that I have created while adding the trains at the end of the program. I want all my new train cars to remain until the user quits. At that point, I want to delete all the new boxes. How do I do that? I tried adding a destructor to my class with "delete next;" and "delete previous;" but the program had a run time error. I am new to pointers and dynamic memory and I want to know how to do that. I will just add functions which I feel are relevant and also main(). Thanks very much :)

#include <iostream>
#include <string>

class train{
   public:
       train(string n);
       bool hasNext();
       bool hasPrevious();
       string getName();
       train* nextTrain();
       train* previousTrain();
       void add(string);
       void detach();
   private:
       string name;
       train* nextTrn;
       train* previousTrn;
};

void train::detach()
{
   if(previousTrn -> hasPrevious())
   {
       previousTrn = previousTrn -> previousTrn;
       delete previousTrn -> nextTrn;
       previousTrn -> nextTrn = this;
   }
   else
       delete previousTrn;
}

train::train(string n)
{
   name = n;
   nextTrn = NULL;
   previousTrn = NULL;
}

void train::add(string n)
{
   train* x = new train(n);
   if(hasPrevious())
   {
       x -> previousTrn = previousTrn;
       x -> previousTrn -> nextTrn = x;
   }
   previousTrn = x;
   x -> nextTrn = this;
}

train* train::nextTrain()
{
   return nextTrn;
}

train* train::previousTrain()
{
   return previousTrn;
}

bool train::hasNext()
{
   if(nextTrn != NULL)
       return true;
   else
       return false;
}

bool train::hasPrevious()
{
   if(previousTrn != NULL)
       return true;
   else
       return false;
}

string train::getName()
{
   return name;
}


int main()
{
   train engine = train("Engine");
   train* current = &engine;
   string choice;
   do
   {
       if(current -> hasNext())
       {
           cout << "Next train: " << current -> nextTrain() -> getName() << endl;
       }

       cout << "Current train: " << current -> getName() << endl;

       if(current -> hasPrevious())
       {
           cout << "Previous train: " << current -> previousTrain() -> getName() << endl;
       }

       cout << "Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, (d)etach a train, or (q)uit? ";
       getline(cin,choice);

       if(tolower(choice[0]) == 'n' && current -> hasNext())
       {
           current = current -> nextTrain();
       }
       else if(tolower(choice[0]) == 'p' && current -> hasPrevious())
       {
           current = current -> previousTrain();
       }
       else if(tolower(choice[0]) == 'a')
       {
           cout << "Which train is this? ";
           string name;
           getline(cin, name);
           current->add(name);
       }  
       else if(tolower(choice[0]) == 'd' && current -> hasPrevious())
       {
           current->detach();
       }          

   }while(tolower(choice[0]) != 'q');

   current = &engine;
   while(current -> hasPrevious())
   {
       current = current -> previousTrain();
       delete current -> nextTrain();
   }
   delete current;
}

Explanation / Answer

#include <iostream>

using namespace std;

class train

{

            public:

            train(string);

            ~train();

            string getName();

            train* nextTrain();

            train* previousTrain();

            bool hasNext();

            bool hasPrevious();

            void add(string);

            void detach();

           

            private:

            string name;

            train* next;

            train* previous;

};

train::train(string str)

{

            name = str;

            next = NULL;

            previous = NULL;

}

train::~train()

{

    delete this -> previous;

}

           

string train::getName()

{

            return name;

}

train* train::nextTrain()

{

            return next;

}

train* train::previousTrain()

{

            return previous;

}

bool train::hasNext()

{

            return next;

}

bool train::hasPrevious()

{

            return previous;

}

void train::add(string str)

{

            train* temp = new train(str);

           

            temp -> next = this;

            temp -> previous = previous;

           

            if(previous)

            {

                        previous -> next = temp;

            }

           

            previous = temp;

}

void train::detach()

{

            if (!previous)

            {

                        return;

            }

           

            if (!(previous -> previous))

            {

                        previous = NULL;

            }

           

            else if (previous -> previous)

            {

                        previous -> previous -> next = this;

                        previous = previous -> previous;

            }

}

           

int main()

{

            train engine = train("Engine");

            train* current = &engine;

            string choice;

            do

            {

                        if(current -> hasNext())

                        {

                                    cout << "Next train: " << current -> nextTrain() -> getName() << endl;

                        }

                       

                        cout << "Current train: " << current -> getName() << endl;

                        if(current -> hasPrevious())

                        {

                                    cout << "Previous train: " << current -> previousTrain() -> getName() << endl;

                        }

                       

                        cout << "Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, (d)etach a train, or (q)uit? ";

                        getline(cin,choice);

                       

                        if(tolower(choice[0]) == 'n' && current -> hasNext())

                        {

                                    current = current -> nextTrain();

                        }

                        else if(tolower(choice[0]) == 'p' && current -> hasPrevious())

                        {

                                    current = current -> previousTrain();

                        }

                        else if(tolower(choice[0]) == 'a')

                        {

                                    cout << "Which train is this? ";

                                    string name;

                                    getline(cin, name);

                                    current->add(name);

                        }          

                        else if(tolower(choice[0]) == 'd')

                        {

                                    current -> detach();

                        }

                        else if(tolower(choice[0]) == 'q')

                        {

                                    current = &engine;

                         delete current;

                }

                       

   }while(tolower(choice[0]) != 'q');

}