Passenger Seat issues

Im working on a program that will let you reserve plane seats and most of its working fine, except it won't mention if there's an invalid input or if a seat's already been reserved under certain conditions. It just says that the seat is taken every time. What am I doing wrong?
Here's the 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
74
75
76
77
78
79
80
81
82
83
84
  #include <cstdlib>
#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 << "Please enter a seat to return, or N to exit.\n";

	cin >> (in1); // getting first character
	cin >> (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';}
				else if (in2 != arr[index1][0])
				{
					cout << "Seat is taken. Please try again.\n";
				}
				else
				{
					cout << "Invalid input. Please pick another seat.\n";

				}
			}
		}
	}
	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 << "Please enter Y, then enter another seat to return, or N to exit.\n";
	cin >> again;


	}while (toupper(again) == 'Y');
}
The problem is line 56: in2 isn't the number it is actually A...D.

By the way: You do not need most of the loop. You can directly calculate index1/index2 from in1/in2:

1
2
index1 = in1 - '1';
index2 = in2 - 'A';

Thus you don't need the loop on line 16/26/50.
So do I edit the loops or do I delete them
I modified your code so that your current problem is solved:
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
  #include <cstdlib>
#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 << "Please enter a seat to return, or N to exit.\n";

	cin >> (in1); // getting first character
	cin >> (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'; break; } // Note: break required
				else if (in1 != arr[index1][0]) // Note: in1 instead of in2
				{
					cout << "Seat is taken. Please try again.\n";
					break; // Note: break required
				}
				// Note: check for invalid input/'N' before you start this loop
			}
			break;
		}
	}
	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 << "Please enter Y, then enter another seat to return, or N to exit.\n";
	cin >> again;


	}while (toupper(again) == 'Y');
}
That just gives me no message for invalid or reserved input. And why did you delete the invalid message anyway
Last edited on
Sorry for the late reply.

And why did you delete the invalid message anyway
Because it is the wrong place for it. Within the loop on line 50 you search for the row. The else on line 60 in your OP would then state "Invalid input.[...]" six times even though it is a valid seat. Hence you cannot use that else which means you need to check the validity before the loop.

If you'd use the calculation I showed above you wouldn't need the loop at all and just the validity check.
Topic archived. No new replies allowed.