so im attempting to assign a second(possibly third value/property) to my linked list of cars. I have a random number of cars added to the list and I need to assign each a random destination, my code seems ok but with my copy/paste skills I cant seem to get my display for each item to display more than just their number. heres my code.
my header file:
class car
{
public:
car(){next=NULL;}
car(int i){id=i; next=NULL;}
void setid(int i){id=i;}
int getid(){return id;}
void setdest(char C){dest=C;}
int getdest(){return dest;}
void setnext(car * n){next = n;}
car * getnext(){return next;}
/*void makenew(){cout<<"new car id? ";
int i;
cin>>i;
next = new car(i);
}*/
void display(){cout<<"Car #"<<id<<" Destination= "<< C <<endl;}
private:
int id;
int dest;
char C;
car * next;
and my main.cpp
(omitted some declarations to make this easier to read)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
for (int i=1; i<1+j; i++)
{
char Let='A';
char C;
x=rand()%3;
C=Let+x;
Sptr=new car(i); //make a car
train.push_front(*Sptr); //add that to the train
}
sitr=train.begin(); //start at the beginning of train
while(sitr!=train.end() )
{ sitr->display(); //show each car
sitr++; //move down the linked list
}
the output I get is sord of whacky.
Input the number of cities for the simulation(less than 5): 4
There are 8 cars in the train
Car #8 Destination= s
Car #7 Destination=
Car #6 Destination= W
Car #5 Destination= E
Car #4 Destination= _
Car #3 Destination= K
Car #2 Destination= H
Car #1 Destination= o
Press any key to continue . . .
Any help would be greatly appreciated and apologies for making such a long post!
I started with a random destination A, B, or C in the for loop, how would I get the destination to display in the output, or is my approach completely wrong?