Airplane Seating Problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Airplane Seating Problem

  1. #1
    Join Date
    Dec 2011
    Posts
    1

    Airplane Seating Problem

    I am need to write a program that seats people in an airplane. There is 7 rows with four seats each labeled A B C D. It need to ask the user what row and seat they want to sit in(1b, 6d, etc). Then it puts an X on that seat and updates it and displays it on the screen. If the user inputs a seat that has already been taken then it will say choose a different seat. This goes until all seats are filled or the user ends the program.
    I think I have everything done correctly so far but when it updates, it doesnt show the X. How can I access the array by what the user enters and change the element to an X? Please help me with any problems you see. Code suggestions would be nice but any help would be great

    Here is my code so far:

    #include <iostream>
    #include <string>
    using namespace std;
    class seating
    {
    char seats[7][4];
    public:
    char choice[2];
    seating()
    {
    for(int i=0;i<7;i++)
    {
    seats [i][0]='A';
    seats [i][1]='B';
    seats [i][2]='C';
    seats [i][3]='D';
    }
    }

    void getchoice()
    {
    cout << endl << "Please enter the seat to reserve (enter x to exit): ";
    cin >> choice;
    }
    void updateseats()
    {
    int row, seat;
    bool goodseat;
    }
    void showseats()
    {
    cout<<endl;
    for(int i=0;i<7;i++)
    {
    cout<<i+1<<" ";
    cout<<seats [i][0]<<" ";
    cout<<seats [i][1]<<" ";
    cout<<seats [i][2]<<" ";
    cout<<seats [i][3];
    cout<<endl;
    }
    }
    };

    int main()
    {
    seating s;
    s.showseats();
    s.getchoice();
    while(s.choice[0]!='x')
    {
    s.updateseats();
    s.showseats();
    s.getchoice();
    }
    cout<<endl<<"Thank you and good bye.";
    cout<<endl<<endl;
    system ("pause");
    return 0;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Airplane Seating Problem

    First problem: You have defined choice as a char[2]. The first major issue here is that you expect the user to enter, for instance, "1c", which requires 3 characters to store {'1', 'c', 0}. The second is that the user may enter something else entirely, like "Hello", and if they do they will totally trash your variables. In the worst case, they could use a buffer overflow attack to gain control of your program and make it do anything they want.

    If your teacher allows it, I would recommend making choice a std::string as this will fix both problems. You should verify that a valid seat is entered, however. If you can't used strings, then you should explicitly limit the number of characters that can be read. This is easy to do using scanf, but I don't recall the exact mechanism for cin right now.

    After you have the input, you need to interpret it. Validate that only 2 characters were entered, that the first one is a digit between '1' and '7', and that the second is a letter either between 'A' and 'D' or between 'a' and 'd'. Finally, you need to convert these characters to indexes.

    For the digit, you should subtract the character '1' to get an index between 0 and 6. For the letter, force it to lowercase using tolower, then subtract 'a' to get an index between 0 and 3.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center