C++ Programming: Fill in the missing functions/classes to enable the program to
ID: 3776770 • Letter: C
Question
C++ Programming:
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.
This is the program that I need to add functions/classes:
This is sample output that I should get after I finish this assignment:
Example 1 (user input is underlined): Current train Engine Do you wish to go to the (n) ext train, (p)revious train, (a) dd a train, or (q) uit? Which train is this? 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? Which train is this? Current train Engine Previous train 1 Do you wish to go to the (n) ext train, (p)revious train, (a) dd aExplanation / Answer
#include <iostream>
#include<string>
using namespace std;
class train
{
private:
string name;
train *next;
train *prev;
public:
train(string n)
{
name=n;
next=NULL;
prev=NULL;
}
void add(string s)
{
train t1(s);
train *t = new train(s);
if(prev == NULL)
{
prev = t;
prev->next=this;
}
else
{
train* temp=prev;
prev=t;
t->prev=temp;
t->next=this;
temp->next=t;
}
}
string getName()
{
return name;
}
bool hasNext()
{
if(next!=NULL) return true;
else return false;
}
bool hasPrevious()
{
if(prev!=NULL) return true;
else return false;
}
train* nextTrain()
{
return next;
}
train* previousTrain()
{
return prev;
}
};
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, 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);
}
}while(tolower(choice[0]) != 'q');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.