Program Termination

This is a discussion on Program Termination within the C++ Programming forums, part of the General Programming Boards category; If you have a look at thise code, how could I terminate the program as mentioned in the example? Code: ...

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    Program Termination

    If you have a look at thise code, how could I terminate the program as mentioned in the example?

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    
    // Prototypes
    void firstClass( int [], int );
    void economyClass( int [], int );
    void boardingPass( int, int );
    
    int main()
    {
       const int numberSeats = 9;
       int airlineSeats[ numberSeats ] = { 0 };
       int choice = 0;
       
       do {
          cout << "\nPress 1 for \"First Class\""
               << "\nPress 2 for \"Economy Class\""
               << "\nPress 3 to Quit"
               << "\n\nEnter your choice: ";
          cin >> choice;
       
          switch( choice )
          {
             case 1:
                      firstClass( airlineSeats, numberSeats );
                      break;
             case 2: 
                      economyClass( airlineSeats, numberSeats );
                      break;
             case 3:
                      return 0;
                      break;
             default:
                      cout << "Woops! Try again.\n";
          }
       } while( choice != 3 );
       
       cin.get();
       return 0;
    }
    
    void firstClass( int seat[], int size )
    {
       static int placeFirst = 0;
       const int type = 1;
       char otherTicket;
       
       if( placeFirst > size - 5 )
       {
          cout << "\nFirst Class is unfortunately fully booked!\n"
               << "Would you like an \"Economy Class\" ticket? [y/n] ";
          cin >> otherTicket;
          
          if( otherTicket == 'y' )
             economyClass( seat, size );
          else
             cout << "\nNext flight leaves in 3 hours.";
    I want the program to terminate here 
       }
       else   
       {
          seat[ placeFirst ] = 1;
          ++placeFirst;
          boardingPass( type, placeFirst );
       }
    }
    
    void economyClass( int seat[], int size )
    {
       static int placeEcon = 5;
       const int type = 2;
       char otherTicket;
       
       if( placeEcon > size )
       {
          cout << "\nEconomy Class is unfortunately fully booked!\n"
               << "Would you like a \"First Class\" ticket? [y/n] ";
          cin >> otherTicket;
          
          if( otherTicket == 'y' )
             firstClass( seat, size );
          else
             cout << "\nNext flight leaves in 3 hours.";
    I want the program to terminate here 
       }
       else   
       {
          seat[ placeEcon ] = 1;
          ++placeEcon;
          boardingPass( type, placeEcon );
       }
    }
    
    void boardingPass( int firstSecond, int seating )
    {   
       cout << "\n**************************"
            << "\nBOARDING PASS"
            << "\n\n";
            
       if( firstSecond == 1 )
          cout << "First Class\n";
       else
          cout << "Economy Class\n";
          
       cout << "Seat number: " << seating
            << "\n**************************\n";
    }
    As always, help is appreciated.

  2. #2
    Registered User
    Join Date
    Jan 2004
    Posts
    10
    You can use the exit(0) function in cstdlib.
    Not exactly sure where you wanted it but just put it there.
    #include "Sig.h"
    #ifndef noob
    #define noob keybone
    #endif

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    If you want the program to actually terminate, return your termination flag (3) and put it in choice:
    Code:
    choice = economyClass( airlineSeats, numberSeats );
    But if you are going to do that, best to not "hard code" the value. Set up your 3 values as #defines
    Code:
    #define EXIT 3
    #define FIRST 1
    #define ECONO 2
    and use the alias. This way if you wish to change EXIT to 0 you don't have to search thru your code to find all the values.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    3
    You can raise exception

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Abnormal program termination
    By Kayoss in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2006, 03:29 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. quick termination of program
    By jobolikescake in forum C Programming
    Replies: 7
    Last Post: 01-20-2002, 09:59 PM
  4. abnormal program termination
    By ProLin in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2002, 08:56 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21