Thread: Help me..

  1. #1
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37

    Help me..

    I want to make a program in order to book airplane's seats.
    My idea was that the user have to type 1 for first seat, 2 for economical seat and 0 for exit..
    so i begin, i thought that i have to use an array, switch-case, with two functions, one for 1-5 seats booking, and another with 6-10, but i was confused..
    My work so far:
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int function_a(int[]);
    
    int main()
    {
    const int size = 10;
    int myArray[size];
    unsigned short int num;
    
    for (int i=0;i<size;i++)
    {
        myArray[i]=0;
    }
    
    cout<<"Please, type 1 for First seat, 2 for Economical seat or 0 to exit:"<<endl;
    cin>>num;
    
    switch(num)
    {
        case 1:cout<<"1 seat taken in element:"<<function_a(myArray);
        break;
        case 0:
            return 0;
            break;
        
        
    }  
      cout<<endl;return main();
                 
      system("PAUSE");	
      return 0;
    }
    
    int function_a(int array[])
    {
        int element;
        for (int j=0;j<6;j++)
        {
            
            element=array[0+j]=1;
            
        }
        
    return element;
    }
    my problem is that i can not figure a method in order to book 1 seat and go to another..

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Is it the airline booking program time of year again? My my, how time flies.

    >my problem is that i can not figure a method in order to book 1 seat and go to another..
    Have you considered assigning seats in first come first serve sequential order? Then you can simply mark the first available seat.
    My best code is written with the delete key.

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    I think this program could be written without the arrays. I'll try to figure it out..

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I think this program could be written without the arrays. I'll try to figure it out..
    Why on Earth would you want to?

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Is this what you're trying to do? Compile and run the code below to see if this is what you were trying to do.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
        const int rowsPerPlane = 20;
        int num;
        int rowsFull;
        int seatsFull1=0;
        int seatsFull2=0;
        int currRow1=1;
        int currRow2=1;
        while(rowsFull != rowsPerPlane)
        {
    
            cout<< "Enter 1 for economical seating, 2 for first class seating, or 0 to quit."<<endl;
            cin>>num;
            
            if(num==1&&seatsFull1!=76)
            {
                seatsFull1+=1;
    
                if(seatsFull1==76)
                {
                    cout<<"Economy Class seating is full."<<endl;
                    continue;
                } 
                
                cout<<"You bought seat "<<seatsFull1<<" in row "<<currRow1<<" of Economy Class."<<endl;
                if(seatsFull1%5==0)
                {
                    currRow1+=1;
                    rowsFull+=1;
                }    
            }
        
            if(num==2&&seatsFull2!=26)
            {
                seatsFull2+=1;
    
                if(seatsFull2==26)        
                {
                    cout<<"First Class seating is full."<<endl;
                    continue;
                }
                
                cout<<"You bought seat "<<seatsFull2<<" in row "<<currRow2<<" of First Class."<<endl;
                if(seatsFull2%5==0)
                {
                    currRow2+=1;
                    rowsFull+=1;
                }    
            }
    
        }
        cout<<"Plane full."<<endl;
        system("PAUSE");
        return 0;
    }
    Last edited by homeyg; 12-06-2004 at 10:28 PM.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Ahh... that does make things simpler, but you lose a lot of the ability to store information on a specific seat. Like whether or not it is full, etc... Perhaps it will allow you to fulfill the requirements of the assignment, but if one can go above and beyond, it's often a good idea.

    Good suggestion though, thanks.

  7. #7
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    good idea homeyg, i am trying to convert it into arrays. The problem is that i have to use 1 dimension array..

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    use a 1d array to represent seats. Initialize array to a given default value indicating open seats. Indexes 0-4 represents seat numbers 1-5--first class and indexes 5-9 represent second class numbers 6-10. In the switch statement when you call the functions you can look up which seats are available and notify user. When user indicates which seat they want you can change the default value of the appropriate index to indicate seat is taken.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    #define TAKEN true
    #define FREE false
    #define N_1ST_CLASS_SEATS 20
    #define N_TOTAL_SEATS 200
    #define IS1STCLASS(idx) (0<=(idx)&&(idx)<N_1ST_CLASS_SEATS)
    #define ISREGULAR(idx) (N_1ST_CLASS_SEATS<=(idx)&&(idx)<N_TOTAL_SEATS)
    
    bool seats[N_TOTAL_SEATS]={0};//set all to false: false ->free, true->taken
    
    //reserving a normal seat
    for(int i=0;i< N_TOTAL_SEATS;i++)
        if(seats[i]== FREE && ISREGULAR(i)){
            seats[i]=TAKEN;
            break;//seat reserved no need to cotinue cicle
        }
    Last edited by xErath; 12-07-2004 at 02:21 PM. Reason: break!

  10. #10
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    i am not sure what functions to use in the switch statement..
    Code:
    function_1(int a[],int b)
    {
     for (int i=0;i<6;i++)
     {
         b=0;
         a[i]=b;
         b=i+1;
     }
     return b;
    }
    this always return 1 seat taken, but if the user type first class again without terminating the program, it must return 2 seat taken etc..how can i do it?

Popular pages Recent additions subscribe to a feed