Home Segments Index Top Previous Next

569: Mainline

Although the amended for loop does the job, it is inelegant. You might wonder whether you can make the output operator, <<, display appropriate characters when given a dereferenced railroad-car pointer, just as it displays appropriate characters when given objects belonging to built-in data types, such as integers. Then, you could write the icon-displaying for loop this way:

// Display icon for first car, no hyphen: 
cout << *train[0]; 
// Display icons for other cars, hyphen included: 
for (n = 1; n < car_count; ++n) { 
  cout << "-" << *train[n]; 
}