Airplane Seating Validation Problem

This is probably right in front of me but I just can't seem to see it.

Background: I'm writing code that will simulate a small airplane seating arrangement. I have all the basic code in order and working but my Validation Function (checks to see if the seat is already taken) isn't working correctly. Everything compiles and runs. I believe it has something to do with either what I'm passing in or the if/else statement in the ValSeat(). Does anyone know what I'm doing wrong? Sort of new to C++ and I'll keep trying different things to see if I can get it to work correctly.

Code:

#include <iostream>
using namespace std;

bool ValSeat(char seats[][5], char airRow, char airCol);
//Checks to see if the seat is already taken.
int main()
{
const int ROW=7, COLUMN=5;
int r=0, c=0;
char seats[ROW][COLUMN], letter = 'A', num = '1', airRow, airCol, again;

for(r=0; r<ROW; r++) //Assigns (A, B, C, and D) to all the columns except the one with numbers.
{
for(c=1; c<COLUMN; c++)
{
seats[r][c] = letter;
letter++;
}
letter ='A'; //Sets the rows to start at the letter A.
}

for(r=0; r>ROW; r++) //Assigns 1-7 to the rows.
{
seats[r][0] = num;
num++;
}

for(r=0; r<ROW; r++) //Displays the seats on the plane.
{
for(c=0; c<COLUMN; c++)
{
cout << seats[r][c] << " ";
}
cout << endl;
}
cout << endl;

do //User picks which seat they want to assign.
{
cout << "Please select the seat you want to assign. (Examples: 1C, 5D, 6B)" << endl;

do
{
cin >> airRow; //Determines which row.
cin >> airCol; //Determines which column (A, B, C, or D).
airCol = toupper(airCol); //This function changes airCol to a capital letter.

for(r=0; r=ROW; r++)
{
if(airRow == seats[r][0])
{
for(c=1; c<COLUMN; c++)
{
if(airCol == seats[r][c])
seats[r][c] = 'X';
}
}
}
}while(!ValSeat(seats, airRow, airCol));

for(r=0; r<ROW; r++) //Shows which seats are taken after a (X) has been added.
{
for(c=0; c<COLUMN; c++)
{
cout << seats[r][c] << " ";
}
cout << endl;
}
cout << endl;

cout << "Would you like to assign another seat? (Y) for Yes or (N) for No." << endl;
cin >> again;
}while(again == 'Y' || again == 'y'); //Checks to see if the user is going to assign another seat.

return 0;
}
bool ValSeat(char seats[][5], char airRow, char airCol)
{
if(seats[airRow][airCol] == 'X')
{
cout << "This seat is already taken. Please select another one" << endl;
return false;
}
else
return true;
}

*Note* Had to copy this from VI so I may have missed something small.

Thanks for taking the time to look.
Last edited on
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
using namespace std;

bool ValSeat(char seats[][5], char airRow, char airCol);
//Checks to see if the seat is already taken.
int main()
{
const int ROW=7, COLUMN=5;
int r=0, c=0;
char seats[ROW][COLUMN], letter = 'A', num = '1', airRow, airCol, again;

for(r=0; r<ROW; r++) //Assigns (A, B, C, and D) to all the columns except the one with numbers.
{
for(c=1; c<COLUMN; c++)
{
seats[r][c] = letter;
letter++;
}
letter ='A'; //Sets the rows to start at the letter A.
}

for(r=0; r>ROW; r++) //Assigns 1-7 to the rows.
{
seats[r][0] = num;
num++;
}

for(r=0; r<ROW; r++) //Displays the seats on the plane.
{
for(c=0; c<COLUMN; c++)
{
cout << seats[r][c] << " ";
}
cout << endl;
}
cout << endl;

do //User picks which seat they want to assign.
{
cout << "Please select the seat you want to assign. (Examples: 1C, 5D, 6B)" << endl;

do
{
cin >> airRow; //Determines which row.
cin >> airCol; //Determines which column (A, B, C, or D).
airCol = toupper(airCol); //This function changes airCol to a capital letter.

for(r=0; r=ROW; r++)
{
if(airRow == seats[r][0])
{
for(c=1; c<COLUMN; c++)
{
if(airCol == seats[r][c])
seats[r][c] = 'X';
}
}
}
}while(!ValSeat(seats, airRow, airCol));

for(r=0; r<ROW; r++) //Shows which seats are taken after a (X) has been added.
{
for(c=0; c<COLUMN; c++)
{	
cout << seats[r][c] << " ";
}
cout << endl;
}
cout << endl;

cout << "Would you like to assign another seat? (Y) for Yes or (N) for No." << endl;
cin >> again;
}while(again == 'Y' || again == 'y'); //Checks to see if the user is going to assign another seat.

return 0;
}
bool ValSeat(char seats[][5], char airRow, char airCol)
{
if(seats[airRow][airCol] == 'X')
{
cout << "This seat is already taken. Please select another one" << endl;
return false;
}
else
return true;
}


looks better with code tags easier to read

I will now try to help you
Ok so here is the working code,sorry I took a while to come back I left the house
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
using namespace std;

bool ValSeat(char seats[][5], char airRow, char airCol);
//Checks to see if the seat is already taken.
int main()
{
    const int ROW=7, COLUMN=5;
    char seats[ROW][COLUMN];
    char letter = 'A', num = '1',airCol, again;
    int airRow;

    for(int r = 0; r<ROW; r++) //Assigns (A, B, C, and D) to all the columns except the one with numbers.
    {
        for(int c = 0; c<COLUMN; c++)
        {
            seats[r][c] = letter;
            letter++;
        }
        letter ='A'; //Sets the rows to start at the letter A.
    }

    for(int r = 0; r<ROW; r++) //Displays the seats on the plane.
    {
        for(int c =0; c<COLUMN; c++)
        {
            cout << seats[r][c] << " ";
        }
        cout << endl;
    }
    cout << endl;

    do //User picks which seat they want to assign.
    {
        cout << "Please select the seat you want to assign. (Examples: 1C, 5D, 6B)" << endl;

        do
        {
            cin >> airRow; //Determines which row.
            cin >> airCol; //Determines which column (A, B, C, or D).
            airCol = toupper(airCol); //This function changes airCol to a capital letter.
            int counter;

            for(int r = 0; r < ROW; r++)
            {

                counter = 0;

                for(char c = 'A'; c < 'F'; c++)
                {

                    if(r == airRow && c == airCol)
                    {
                        seats[r][counter] = 'X';
                    }
                    counter++;
                }

            }
            if(airRow >= ROW || airCol < 'A' || airCol > 'D')
            {
                cout << "invalid row or invalid col" << endl;
                break;
            }
        }
        while(!ValSeat(seats, airRow, airCol));

        for(int r =0; r<ROW; r++) //Shows which seats are taken after a (X) has been added.
        {
            for(int c=0; c<COLUMN; c++)
            {
                cout << seats[r][c] << " ";
            }
            cout << endl;
        }
        cout << endl;

        cout << "Would you like to assign another seat? (Y) for Yes or (N) for No." << endl;
        cin >> again;
    }
    while(again == 'Y' || again == 'y');  //Checks to see if the user is going to assign another seat.

    return 0;
}
bool ValSeat(char seats[][5], char airRow, char airCol)
{
    if(seats[airRow][airCol] == 'X')
    {
        cout << "This seat is already taken. Please select another one" << endl;
        return false;
    }
    else
        return true;
}


first off the code can be tidied limiting the amount of nested loops you use and also could be broken down into more functions to make things tidier

so the first problem you had was that you set r = row in one of the for loops when you meant to say r < row

second I changed airRow to be an int instead of a char,would help simplify things when using it in loops etc,I implemented a counter variable to count the number of cols so if 1D was selected the counter would be 0 so seats[r][0] would be equal to 'X',

again maybe try and re do the code break it down into more functions,this problem could also be solved using classes,I made a similar program a while back using classes representing seat as a class
Last edited on
also another major problem,I'm referring to the line below
1
2
3

while(!ValSeat(seats, airRow, airCol))


what if the user entered 12 for airRow and K for airCol?

so you would need to have some bounds checking on airRow and airCol,I broke out of the while loop if the user entered a a value higher than the constant ROW or if the user entered a character that was not in the 'A' to 'F' range
Ok, I'll add some boundaries. Thanks for the advice and help! :)
Topic archived. No new replies allowed.