Need help with an airseat program

I am a beginner C++ student and I haven't been doing so well in my class.I was wondering if anyone here could help me possibly since my teacher recommended this site.If you can help, thank you very much.

p.s Could you comment on things if they're "advanced" :)
Here is what the code should do:
Write a program to assign passengers seats in an airplane. Assume a small airplane wih seat numbering as follows:

1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D

The progrram should display the seat pattern, with an 'X' marking the seats already assigned. For example,
after seats 1A, 2B, and 4C are taken, the display should look like this:

1 X B C D
2 A X C D
3 A B C D
4 A B X D
5 A B C D
6 A B C D
7 A B C D

After displaying the seats available, the program prompts for the seat desired, the user types in a seat,
and then the display of available seats is updated. This continues until all seats are filled or until the user signlas that the program should end. If the user types in a seat that is already assigned, the program
should say that that seat is occupied and ask for another choice.

The code I have right now is insufficient so it doesn't work perfectly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string Seat = "";
    string Airline[7][7] = {{"1", "2", "3", "4", "5", "6", "7"}, {"ABCD", "ABCD", "ABCD", "ABCD", "ABCD", "ABCD", "ABCD"}};
    char Again = ' ';
    int Row = 0;
   
    cout << "Enter a row: ";
    cin >> Row;
    cout << "Enter a seat: ";
    cin >> Seat;
    cout << "Enter another seat? (y/n): ";
    cin >> Again;
   
    while(Again == 'y')
    {
                for(int x = 0; x < 7; x++)
                {
                            for(int y = 0; y < 7; y++)
                            {
                                cout << "Enter a seat: ";
                                cin >> Seat;
                                cout << Airline[y] << endl;
                            }
                        cout << "Enter a row: ";
                        cin >> Row;
                        cout << Airline[x] << endl;
                }
    } //end while
   
    system("PAUSE");
    return 0;
}
before any other thing please read the second topic
http://www.cplusplus.com/forum/beginner/1988/
Last edited on
I'm not sure what I'm supposed to see there.. :S
closed account (D80DSL3A)
I would use a simpler array of strings for the seats:
string Airline[7] = { "1ABCD", "2ABCD", "3ABCD", "4ABCD", "5ABCD", "6ABCD", "7ABCD" };
You could then display the seating arrangement like so:
1
2
for(int i=0; i<7; ++i)
		cout << Airline[i] << endl;

Why use a string for the Seat variable? Seat is just a single character value.
If you have char Seat, get the value from the user:
1
2
3
4
cout << "Enter a row: ";
    cin >> Row;
    cout << "Enter a seat: ";
    cin >> Seat;

Then use these values to see if the requested seat is available.
With the Airline[] of strings above you could compare the requested seat to the appropriate character in the Airline string like so:
1
2
3
4
5
int idx = 1 + Seat - 'A';
if( Airline[Row-1][idx] == 'X' )
     // seat is not available - prompt user for another
else
    // seat IS available. Assign Airline[Row-1][idx] = 'X'; to update the seating chart. 

Hope this helps get you started. Note: Your while loop starting on line 18 would never end since nothing within the loop changes the value of Again.

@horance89
While it is true that using system("pause") is frowned upon here it is NOT what is keeping OP's program from working. It is a side issue, not something which should come "before any other thing".
Thank you very much for the help. Greatly appreciated :)

I just have one problem, when I enter the seat and row, it only fills out that seat with the X and ends the program.

Updated version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
using namespace std;
int main()
{
    int Seat = 0;
    string Airline[7] = { "1ABCD", "2ABCD", "3ABCD", "4ABCD", "5ABCD", "6ABCD", "7ABCD" };
    char Again = ' ';
    int Row = 0;
   
    cout << "Enter a row: ";
    cin >> Row;
    cout << "Enter a seat: ";
    cin >> Seat;
    
    int idx = 1 + Seat;
    if(Airline[Row-1][idx] == 'X')
        cout << "Seat is taken." << endl;
    else 
         Airline[Row - 1][idx] = 'X';
        
    for(int i=0; i<7; ++i)
    {
		cout << Airline[i] << endl;
    }
    cout << "Enter another seat? (y/n): ";
    cin >> Again;
   
    while(Again == 'y')
    {
                for(int x = 0; x < 7; x++)
                {
                            for(int y = 0; y < 7; y++)
                            {
                                cout << "Enter a seat: ";
                                cin >> Seat;
                                cout << Airline[y] << endl;
                            }
                        cout << "Enter a row: ";
                        cin >> Row;
                        cout << Airline[x] << endl;
                }
                cout << "Enter another seat? (y/n): ";
                cin >> Again;
    } //end while
    
    system("PAUSE");
    return 0;
}
Found another problem, when I enter a seat number, it always put 'X' in the '1' position and it's still not letting me enter another seat.
closed account (D80DSL3A)
I tried your code. It enters the while() loop as expected if I enter y after lines 26 and 27
1
2
cout << "Enter another seat? (y/n): ";
    cin >> Again;

are executed, so I don't know what you mean that it won't let you enter another seat.

The for loops are then entered which results in 49 prompts to enter Seat or Row. Is this intended?
Line 43 will take a while to get to! What is the point of the for loops?

I see you decided to make Seat an integer variable instead of a char. Why? It works if user enters 0,1,2 or 3 for Seat though. I thought you wanted to enter A,B,C or D for that.

I hope you have made more progress by now (it's been a few more hours).
Ok I took out the for loops, forgot to take them out before.
I changed seat to an 'int' because they enter a number for the seat variable.

Row is a string because it takes in a letter. I could change it to 'char' instead I suppose.

Here is the updated version I got so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
using namespace std;
int main()
{
    int Seat = 0;
    string Airline[7] = {"1ABCD", "2ABCD", "3ABCD", "4ABCD", "5ABCD", "6ABCD", "7ABCD"};
    char Again = ' ';
    int Row = 0;
  
    cout << "Enter a row: ";
    cin >> Row;
    cout << "Enter a seat: ";
    cin >> Seat;
   
            int idx = Seat + 1;
            if(Airline[Row - 1][idx] == 'X') { //checks if seat is taken or not
                  cout << "Seat is taken." << endl;
                  cout << "Enter a row: ";
                  cin >> Row;
                  cout << "Enter another seat: ";
                  cin >> Seat; }
            else
                  Airline[Row - 1][idx] = 'X';
       
    for(int i = 0; i < 7; i++)
    {
        cout << Airline[i] << endl; //displays the array
    }
    cout << "Enter another seat? (y/n): ";
    cin >> Again;
  
    while(Again == 'y')     //loop is not executed
    {
                cout << "Enter a row: ";
                cin >> Row;
                cout << "Enter a seat: ";
                cin >> Seat;
               
                for(int idx = 0; idx < 7; idx++)
                {
                        int idx = 1 + Seat;
                        if(Airline[Row-1][idx] == 'X') //checks if seat is taken or not
                               cout << "Seat is taken." << endl;
                        else
                               Airline[Row - 1][idx] = 'X';
                } 
               
                cout << "Enter another seat? (y/n): ";
                cin >> Again;
    } //end while
   
    cout << "Bye!" << endl;
   
    system("PAUSE");
    return 0;
}


Here is what I meant by when I said it doesn't execute the while loop: http://i1098.photobucket.com/albums/g377/CStu/CError.jpg


Thanks for helping so far. :)
Last edited on
closed account (D80DSL3A)
Thanks for the screenshot. It fully explains the problem.
Here's the problem: You declared Seat to be an int then you entered a C when prompted for the value. The letter C is not a legal value for an integer. This caused a failure condition for the input stream (cin) so it won't read in anything else after that.
The value of Seat remained 0 (it's initial value) since an illegal value was entered for it.
This is why you got an X for seat 5A.

You need to decide which data type Seat is going to be and then enter an appropriate value for it.

You're welcome for the help.
PS Your latest code above still contains a senseless for loop within the while body. What is this for?

PPS I will try to help more this time, so hopefully you can get your program done.
If you initialize Again = 'y'; instead of Again = ' '; you could eliminate lines 11 through 31 completely.
The while loop would be entered and all cases (including the first) could be handled there.
Get rid of the for loop and think what a well placed continue; could be used for.
Last edited on
Ah I see, silly mistake. I changed it to an 'int' value.
But when I go compile, I get an error right here:
 
int idx = 1 + Seat - 'A';


It says "no match for 'operator+' in '1 + Seat". I tried putting "Row" in place of seat but it says it's shutting down for an unusual problem.
closed account (D80DSL3A)
Sorry, I was still editing my last reply when you posted last.
You entered an int value for Seat now when using the program?
If Seat is an int then the line:
int idx = 1 + Seat - 'A';
is no longer appropriate. That line was meant for Seat as a char value.
I am not sure what to replace the '[idx]' with so I took it out. Here is an updated version and now it's giving me an error on this line:

1
2
3
4
5
6
           if(Airline[Row-1] == 'X') { //checks if seat is taken or not
                               cout << "Seat is taken." << endl;
                               continue; }
                        else
                               Airline[Row - 1] = 'X';
             


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

using namespace std;
int main()
{
    string Seat = 0;
    string Airline[7] = {"1ABCD", "2ABCD", "3ABCD", "4ABCD", "5ABCD", "6ABCD", "7ABCD"};
    char Again = ' ';
    int Row = 0;
  
    Again = 'y';
  
    while(Again == 'y')     //loop is not executed
    {
                cout << "Enter a row: ";
                cin >> Row;
                cout << "Enter a seat: ";
                cin >> Seat;
               
                        if(Airline[Row-1] == 'X') { //checks if seat is taken or not
                               cout << "Seat is taken." << endl;
                               continue; }
                        else
                               Airline[Row - 1] = 'X';
               
                cout << "Enter another seat? (y/n): ";
                cin >> Again;
    } //end while
   
    cout << "Bye!" << endl;
   
    system("PAUSE");
    return 0;
}
Last edited on
closed account (D80DSL3A)
Good job. That's exactly where the continue should go.
Now Seat is a string???
Notice that you aren't even using the value of Seat now.
Why the change from Airline[Row - 1][idx] to Airline[Row - 1]? This is where your new problem is coming from.
It works! Thanks for all the help :D

Final product:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>

using namespace std;
int main()
{
    char Seat = ' ';
    string Airline[7] = {"1ABCD", "2ABCD", "3ABCD", "4ABCD", "5ABCD", "6ABCD", "7ABCD"};
    char Again = ' ';
    int Row = 0;
  
    Again = 'y';
  
    while(Again == 'y')
    {
                cout << "Enter a row: ";
                cin >> Row;
                cout << "Enter a seat: ";
                cin >> Seat;
               
                        int idx = 1 + Seat - 'A';
                        if(Airline[Row-1][idx] == 'X') { //checks if seat is taken or not
                               cout << "Seat is taken." << endl;
                               continue; }
                        else
                               Airline[Row - 1][idx] = 'X';
                               cout << "Enter another seat? (y/n): ";
                               cin >> Again;
                               
               for(int i=0; i<7; ++i)
               {
		               cout << Airline[i] << endl;
               }
               
    } //end while
   
    cout << "Bye!" << endl;
   
    system("PAUSE");
    return 0;
}
Last edited on
closed account (D80DSL3A)
Congrats! You're welcome.
Topic archived. No new replies allowed.