Home Segments Index Top Previous Next

473: Mainline

Now suppose that you want to take more drastic action, terminating your program whenever it encounters an unknown car-type number. To arrange for an orderly termination, you need to inform C++ that you intend to make use of the exit function by including the following line in your program:

#include   

Then, you can insert a call to the exit function after a output statement that displays data via the cerr stream:

switch (type_code) { 
  case 0: train[n] = new engine;   break; 
  case 1: train[n] = new box_car;  break; 
  case 2: train[n] = new tank_car; break; 
  case 3: train[n] = new caboose;  break; 
  default: cerr << "Car code " << type_code 
                << " is unknown!" << endl;  
           exit (0); 
} 

Note that exit expects an integer argument. That integer argument generally should be 0, thereby indicating that your program's termination is not to evoke a special reaction from your operating system.