Airplane seating problem using arrays/strings

Program asks to assign seats on a small airplane that has 7 rows and 4 columns. after a seat has been selected by user input, an X is to appear in that spot and the new display is to be displayed. I've searched online for answers for hours. But I just can't grasp the concept.

The issue I'm having is one entire row is being chosen for whatever row I choose, instead of just one spot in one row. And I'm not sure how to display an updated chart for each new taken spot.

Here's my code 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
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
using namespace std;
const int ROWS = 7;
const int COLUMNS = 4;
int main() 
{

    
    char again;
    int row;
    char seat;
    string arraySeats[ROWS][COLUMNS] = {{"A", "B", "C", "D"}, 
                                    {"A", "B", "C", "D"},
                                    {"A", "B", "C", "D"}, 
                                    {"A", "B", "C", "D"},
                                    {"A", "B", "C", "D"},
                                    {"A", "B", "C", "D"}, 
                                    {"A", "B", "C", "D"}};
    
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            cout << setw(5) << arraySeats[i][j] << " ";
        }
        cout << endl;
    }
    
    
    do {
        cout << "\nEnter a row 1-7." << endl;
        cin >> row;
        cout << row << endl;
        cout << "Enter a seat, A B C D." << endl;
        cin >> seat;
        cout << seat << endl;
                
        row = ROWS - '1';
        seat = COLUMNS - 'A';
        
        if (arraySeats[row][seat] == "X")
        {
            cout << "\nThat seat is taken. Pick another." << endl;
        }
        else
        {
            cout << "Seat " << seat << " is now claimed." << endl;
            arraySeats[row][seat] = "X";
        }
        
        for (int i = 0; i < ROWS; i++)
        {
            for (int j = 0; j < COLUMNS; j++)
            {
                cout << setw(5) << arraySeats[i][j] << " ";
            }
            cout << endl;
        }
        
        cout << "\nAgain? Y or y for Yes, any other key for No. " << endl;
        cin >> again;
            
    } while (again == 'Y' || again == 'y');
    
    return 0;
}
row = ROWS - '1';

Should be :
row = row - 1;

row is an int, not a char.
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>
#include <iomanip>

using namespace std;

const int ROWS = 7;
const int COLUMNS = 4;

int main() 
{
    char again;
    
    int row = 0;
    int seat = 0;
    
    char arraySeats[ROWS][COLUMNS] = {
        {'A', 'B', 'C', 'D'},
        {'A', 'B', 'C', 'D'},
        {'A', 'B', 'C', 'D'},
        {'A', 'B', 'C', 'D'},
        {'A', 'B', 'C', 'D'},
        {'A', 'B', 'C', 'D'},
        {'A', 'B', 'C', 'D'}
        };
    
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            cout << setw(5) << arraySeats[i][j] << " ";
        }
        cout << endl;
    }
    
    
    do {
        cout << "\nEnter a row 1-7. ";
        cin >> row;
        row--;
        
        cout << "Enter a seat, A B C D. ";
        cin >> seat;
        seat--;
        
        if (arraySeats[row][seat] == 'X')
        {
            cout << "\nThat seat is taken. Pick another." << endl;
        }
        else
        {
            cout << "Seat " << seat << " is now claimed." << endl;
            arraySeats[row][seat] = 'X';
        }
        
        for (int i = 0; i < ROWS; i++)
        {
            for (int j = 0; j < COLUMNS; j++)
            {
                cout << setw(5) << arraySeats[i][j] << " ";
            }
            cout << endl;
        }
        
        cout << "\nAgain? Y or y for Yes, any other key for No. " << endl;
        cin >> again;
            
    } while (again == 'Y' || again == 'y');
    
    return 0;
}


Array elements are better suited to being char's not strings.

You need to devise a method for converting char A, B C or D to 1, 2 or 4 respectively. Note that char's have an integer value that is the ASCII code for that letter which you can take advantage of for the column number. eg 'A' - 'A' = 0, 'B' - 'A' = 1 ...
@kemort
int seat = 0;

And :
1
2
3
cout << "Enter a seat, A B C D : ";
cin >> seat;
seat--;


No. The user will be tempted to enter A, B, C, or D and will not input an integer. It is indeed quite a miserable mistake.
Last edited on
closed account (48T7M4Gy)
It's not a miserable mistake as the paragraph at the end of my post indicates. I took the decision to leave that part to the OP as a (guided) learning experience.
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
#include <iostream>
#include <iomanip>

using namespace std;

const int ROWS = 7;
const int COLUMNS = 4;

int main() 
{
	int i, j;

    char arraySeats[ROWS][COLUMNS] = 
	{
        {'A', 'B', 'C', 'D'},
        {'A', 'B', 'C', 'D'},
        {'A', 'B', 'C', 'D'},
        {'A', 'B', 'C', 'D'},
        {'A', 'B', 'C', 'D'},
        {'A', 'B', 'C', 'D'},
        {'A', 'B', 'C', 'D'}
    };
    
    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLUMNS; j++)
        {
            cout << setw(5) << arraySeats[i][j] << " ";
        }
        cout << endl;
    }

    char again;
    do 
	{
		int row;
		char seat;

		while(true)
		{
	        cout << "\nEnter a row 1-7 : ";
			if(cin >> row && row >= 1 && row <= 7) {row--; break;}

			if(!cin)
			{
				cin.clear();
				cin.ignore(1000, '\n');
			}
		}

		while(true)
		{		        
			cout << "Enter a seat, A, B, C, or D : ";
			cin >> seat;

			if(seat >= 'A' && seat <= 'D') {seat = seat - 'A'; break;}
			if(seat >= 'a' && seat <= 'd') {seat = seat - 'd'; break;}
		}
        
        if (arraySeats[row][seat] == 'X')
        {
            cout << "\nThat seat is taken. Pick another." << endl;
        }
        else
        {
            cout << "Seat " << row + 1 << (char)(seat + 'A') << " is now claimed." << endl;
            arraySeats[row][seat] = 'X';
        }
        
        for (i = 0; i < ROWS; i++)
        {
            for (j = 0; j < COLUMNS; j++)
            {
                cout << setw(5) << arraySeats[i][j] << " ";
            }
            cout << endl;
        }
        
        cout << "\nAgain? (Y or y to continue) : ";
        cin >> again;
            
    } while (again == 'Y' || again == 'y');
    
    return 0;
}
Last edited on
@SuckKemort
This name sounds good. I will be calling you by that name then.


Reported for harassment. Its no wonder you are so popular on this forum.
Last edited on
@Arslan7041
He provoked me first. Of course now his comment is edited, I have no reason to call him "SuckKemort".

Edit : Previously he did call me "SuckBuster". Is that enough?
Last edited on
He provoked me first. Of course now his comment is edited, I have no reason to call him "SuckKemort".

Edit : Previously he did call me "SuckBuster". Is that enough?


Then both of you were wrong. But I do apologize for thinking it was only you.
closed account (48T7M4Gy)
[quote]Then both of you were wrong[/quote
Really? And under what authority is that pronouncement made?
Topic archived. No new replies allowed.