A program that assign passengers seats

Hi, my assignment is to assign passengers seats in an airplane. assuming the airplane with seat numbering like this:
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 program should show the seat pattern, with an X marking the seats already assigned. For example, after seat 1A, 2B, 4C are taken, the display should be:
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

I know this assignment has been asked several times, and I read some posts about this assignment too, however, none really addresses the problem I am having right now.

(1) My program run fine for the first time, it was able to assign X to ONE seat, however, when I try to repeat the process, it would not assign another X to the second seat.
(2) My program has a do while loop that is supposed to repeat the above process for as long as the user wishes. However, due to unknown reason I am only able to repeat the process one time, and the program terminates after the second loop.

Below is my code:
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
#include <iostream>
using namespace std;


const int num_row = 7;
const int num_column = 5;
int main()
{
	int index1, index2;
	char arr[num_row][num_column];
	char letter = 'A';
	char num = '1';
	char in1, in2, again;

	for (index1 =0; index1 < 7; index1++) // this loop assign A B C D to all 7 rows
	{
		for (index2 = 1; index2 <5; index2++)
		{
			arr[index1][index2] = letter;
			letter++;
		}
	letter = 'A';
	} 

	for (index1= 0; index1< 7; index1++) //This loop assign the number 1 to 7 to the 7 rows
	{
		arr[index1][0] = num;
		num++;
	}

	for (index1 = 0; index1 < 7; index1++) // this loop shows the plane seats
	{
		for (index2 = 0; index2 < 5; index2++)
		{
			cout << arr[index1][index2] << " ";
		}
	cout << endl;
	}
	cout << endl;   
	
	do // this loop repeat the process
	{
	cout << "type a seat \n";

	cin.get(in1); // getting first character
	cin.get(in2);	//getting second character
	in2 = toupper(in2);

	for (index1 = 0; index1 < 7; index1++) //this loop mark X to a seat
	{
		if (in1 == arr[index1][0])
		{for (index2 = 1; index2 < 5; index2++)
			{if (in2 == arr[index1][index2])
				{arr[index1][index2] = 'X';}
			}
		}
	}
	for (index1 = 0; index1 < 7; index1++) //This loop shows whats happen after the seat is marked
	{
	{
		for (index2 = 0; index2 < 5; index2++)
		{
			cout << arr[index1][index2] << " ";
		}
	cout << endl;
	}
	}
	cout << "Again? \n";
	cin >> again;
	

	}while (toupper(again) == 'Y');
}


Thank you very much again and any comments or help is greatly appreciated.
Last edited on
Here are my comments about changes I would make and the resulting code. i haven't tested it too much, so be gentle...
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
95
96
97
98
99
100
// got rid of some magic numbers
// got rid of second loop for adding first column, can all be done in 1 loop
// remember you can define variables just before you use them, you needn't define all variables at the beginning of the scope
// check for cin success
// don't need a loop for marking seats
// changed loop termination condition from a positive to a negative, so spurious input doesn't terminate the loop

#include <iostream>
#include <limits>

using namespace std;

constexpr int num_row = 7;
constexpr int num_column = 5;

int main()
{
	int index1, index2;
	char arr[num_row][num_column];
	char letter = 'A';
	char num = '1';

	for (index1 =0; index1 < num_row; index1++) // this loop assign A B C D to all 7 rows
	{
		arr[index1][0] = index1 + '1';
		for (index2 = 1; index2 < num_column; index2++)
		{
			arr[index1][index2] = letter;
			letter++;
		}
		
		letter = 'A';
	} 

	for (index1 = 0; index1 < num_row; index1++) // this loop shows the plane seats
	{
		for (index2 = 0; index2 < num_column; index2++)
		{
			cout << arr[index1][index2] << " ";
		}
		
		cout << endl;
	}

	cout << endl;   

	do // this loop repeat the process
	{
		cout << "type a seat \n";
		
		// getting first character
		int in1;
		if (!(cin >> in1)) 
		{
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n' );
			continue;
		}
		
		--in1; // remember row 1 is arr[0]
		// getting second character
		char in2;
		if (!(cin >> in2))
		{
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n' );
			continue;
		}
		
		in2 = toupper(in2);

		if (in1 < 0 || in1 >= num_row || in2 < 'A' || in2 > 'D')
		{
			continue;
		}
		
		++in2; // 'A' - 'A' = 0, and we don't want column 0
		
		if ( 'X' == arr[in1][in2-'A'])
		{
			cout << "Seat already taken" << endl;
			continue;
		}
		
		arr[in1][in2-'A'] = 'X';
		
		for (index1 = 0; index1 < num_row; index1++) //This loop shows whats happen after the seat is marked
		{
			for (index2 = 0; index2 < num_column; index2++)
			{
				cout << arr[index1][index2] << " ";
			}
			
			cout << endl;
		}
		
		cout << endl << "Again?" << endl;
	}
	while (toupper(cin.get()) != 'Q');
}


P.S. It's been a long day, and I'm totally spacing on how to handle stream input problems, so double-check that in particular.
Last edited on
Actually... I found the problem to my program... After staring at it for 2 hours.

I actually should not use cin.get, and instead I should just use cin >> to enter the seat...
Now it runs as it should... Thank you so much anyways man!!!!!
Hmm... that shouldn't really make a difference. If it appears to be solving your problem, then take a closer look at the types you're using and how you handle invalid input.

Happy coding :)
Topic archived. No new replies allowed.