Airplane Seating Program Problem

Discussion in 'C++' started by Phalax, Nov 22, 2007.

  1. Phalax

    Phalax New Member

    Joined:
    Nov 22, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello my friends and I have been reading several C++ tutorials online and reading many books, my all time favorite is C++ for dummies because it is simple to understand. Me and my friends are bored and have decided to create a little game, but we have to learn to program before we make that leap. My brother's friend gave us an interesting problem to solve to test our knowledge so far and I would have to say it has not been going to good. What the problem he gave us is we have to create a program that:
    1) Prints out the seating chart of an airplane
    2) Reserve a seat
    3) Unreserve a seat
    4) Exit the program

    Me and my friends have decided that we will be using multidimensional arrays since we have just learned them to print out our seating chart 8x6. Then instead of using a bunch of loops and cout statements we were going to try to save a text file and load that which we kinda are trying to solve that. What we are stuck on is when we ask the user to Reserve a seat or unreserve a seat it will save it and when the user wants to print the seating chart it will show that the seat they reserved is reserved with the character 'R' and any empty seat will be set with 'E' for empty. Also confused on how to enter and print out input from an array? This is what we have so far:

    Code:
    
    #include <iostream>
    #include <fstream>
    using namespace std;
    const unsigned int numrows = 8;
    const unsigned int numseats = 6;
    
    void print();
    void reserve_seat();
    void unreserve_seat();
    void reset_chart();
    void exit();
    
    char seat[8][6];
    bool quit = false;
    
    int main ()
    {
        int menu_option;
    
            for (int x = 0; x < numrows; ++x)
            {
                for (int y = 0; y < numseats; ++y)
                {
                    seat[x][y] = 'E';
                }
            }
       
            cout << "Airplane seat reservation program\n" << endl;
            cout << "1: Print seating chart\n"
                 << "2: Reserve a seat\n"
                 << "3: Un-reserve a seat\n"
                 << "4: Reset seating chart\n"
                 << "5: Exit program\n"
                 << endl
                 << "Make a selection: ";
     while (!quit)
     {            
            cin >> menu_option;
           
            switch (menu_option)
            {
                   case 1:
                          print();
                   break;
                   case 2:
                          reserve_seat();
                   break;
                   case 3:
                        unreserve_seat();
                   break;
                   case 4:
                        reset_chart();
                   break;
                   case 5:
                        exit();
                   break;
                   default:
                           cout << "Wrong Choice Please Choose Again!\n";
                   break;
           }
    }
    system("PAUSE");
    return 0;
    }
    
    void print()
    {
         for (int x = 0; x < numrows; ++x) 
         {
             cout << endl;
             for (int y = 0; y < numseats; ++y)
             {
              cout << seat[x][y];
             }
         }
    }
    void reserve_seat()
    {
    int row;
    char seats;
    int convert = 0;
    cout << "Please Enter The Seat Row and Letter You Would Like To Reserve\n";
    
    for (int x = 0; x < numrows; ++x)
    {
        for (int y = 0; y < numseats; ++y)
        {
            cin >> seat[x][y];
            seat[x][y] = 'R';
        }
    }
    
    
    }
    void unreserve_seat()
    {
         cout << "Unreserve Seat\n";
    }
    void reset_chart()
    {
         cout << "Reset Chart\n"; 
    }
    void exit()
    {
         cout << "Exit\n";    
    }
    

    Just feels like we are heading in the wrong direction and wanted to know if someone could help us out and explain in depth what is happening. We tried looking up several tutorials that goes along with our problem, but we failed to find any good ones.
    We were also thinking about not using the fstream and saving it to a file and loading it because this seems to high advance for our level, but I dunno.
    A B C D E F
    1 E E E E E E
    2 E E E E E E
    3 E E E E E E
    4 E E E E E E
    5 E E E E E E
    6 E E E E E E
    7 E E E E E E

    Thats what we are shooting for, we hope :)

    Thank you for the help.
     
  2. dharmaraj.guru

    dharmaraj.guru New Member

    Joined:
    Oct 23, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    The way you are getting the inputs and the reservation system is wrong..
    You have to recieve two inputs from the user. Your cin statements has to recieve inputs at
    variables row and seats. After getting the inputs, validate them. If the validation is successful, convert them to array indices. Then check whether the requested seat is empty, and finally reserve the ticket.
    Here is the modified function.
    Code:
    void reserve_seat()
    {
    	int row;
    	char seats;
    	int convert = 0;
    	cout << "Please Enter The Seat Row and Letter You Would Like To Reserve\n";
    
    	cout << “Please Enter the seat row(1 - 7)\n”;
    	cin >> row;
    	if(row < 1 || row > 7)
    	{
    		cout << “You have entered invalid row number\n Kindly try again...\n”;
    		return;
    	}
    	else
    	{
    		--row;
    	}
    
    	cout << “Please enter the seat letter\n”;
    	cin >> seats;
    	if(seats < A || seats > F)
    	{
    		cout << “You have entered invalid seat letter.\n Kindly try again”;
    		return;
    	}
    	else
    	{
    		seats -= 'A';
    	}
    
    	if(seats[row][seats] == 'R')	
    	{
    		cout << “Sorry!!..Seat number “<<row<<seats<<” is already reserved\n”;
    		return;
    	}
    	else
    	{
    		seat[row][seats] = 'R';
    		cout << “Seat number “<<row<<seats<<” is reserved successfully\n”;
    	}
    
    	return;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice