Problem A Adding trains Start with the tootTootcpp file Fill
Problem A: Adding trains
Start with the tootToot.cpp file. Fill in the missing functions/classes to enable the program to compile and work correctly. This program simulates a train, where you start with just an engine. You have three options: go to the next train (forward towards the engine), go to the previous train (back towards the end of the train) or add a train behind the current train car. You may not change main(), we will check to ensure it is exactly the same. You should be able to train cars anywhere, and this will insert it into that part of the train. For this part, you do not need to worry about deleting dynamic memory.
Hint: when making the add() function, it would probably help to draw it out and figure out how many arrows/pointers you need to change.
/////////////////////////////////////////////////////////////////////////////
Solution
// tootToot.cpp
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
class train
{
public:
train(string name);
string getName();
bool hasNext();
bool hasPrevious();
void add(string);
void detach();
train* nextTrain();
train* previousTrain();
private:
string train_name;
train* train_next;
train* train_Previous;
};
void train::detach()
{
if(train_Previous -> hasPrevious())
{
train_Previous = train_Previous -> train_Previous;
delete train_Previous -> train_next;
train_Previous -> train_next = this;
}
else
delete train_Previous;
}
train::train(string name)
{
train_name = name;
train_Previous = NULL;
train_next = NULL;
}
void train::add(string name)
{
train* tempTrain = new train(name);
if(hasPrevious())
{
tempTrain -> train_Previous = train_Previous;
tempTrain -> train_Previous -> train_next = tempTrain;
}
train_Previous = tempTrain;
tempTrain -> train_next = this;
}
bool train::hasNext()
{
if(train_next != NULL)
return true;
else
return false;
}
bool train::hasPrevious()
{
if(train_Previous != NULL)
return true;
else
return false;
}
string train::getName()
{
return train_name;
}
train* train::nextTrain()
{
return train_next;
}
train* train::previousTrain()
{
return train_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 -> hasPrevious())
{
current->detach();
}
}while(tolower(choice[0]) != \'q\');
return 0;
}
/*
output:
Example 1 (user input is bolded):
Current train: Engine
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
a
Which train is this?
4
Current train: Engine
Previous train: 4
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
a
Which train is this?
1
Current train: Engine
Previous train: 1
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
p
Next train: Engine
Current train: 1
Previous train: 4
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
a
Which train is this?
3
Next train: Engine
Current train: 1
Previous train: 3
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
a
Which train is this?
2
Next train: Engine
Current train: 1
Previous train: 2
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
p
Next train: 1
Current train: 2
Previous train: 3
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
p
Next train: 2
Current train: 3
Previous train: 4
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
p
Next train: 3
Current train: 4
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
p
Next train: 3
Current train: 4
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
a
Which train is this?
5
Next train: 3
Current train: 4
Previous train: 5
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
p
Next train: 4
Current train: 5
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
n
Next train: 3
Current train: 4
Previous train: 5
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
n
Next train: 2
Current train: 3
Previous train: 4
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
n
Next train: 1
Current train: 2
Previous train: 3
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
n
Next train: Engine
Current train: 1
Previous train: 2
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
n
Current train: Engine
Previous train: 1
Do you wish to go to the (n)ext train, (p)revious train, (a)dd a train, or (q)uit?
q
*/



