Please help, I'm desperate

This is my first topic here, so let me know if I missed information I should have added!

I'm really new to programming, so bear with me.

I've been sitting at this program for nearly five hours now, but I just can't figure it out and I'm really miserable and discouraged at this point, so any help would be really appreciated because I need it.

The problem is as so:

Write a program to assign passengers seats in an airplane. Assume a small airplane with 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 program 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 set, and then the display of available seats is updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that the seat is occupied and ask for another choice.

-----------------------------------------------

The program I have so far is below. It works fine. The only thing that doesn't is that when all the seats are filled it will still ask the user if they want to fill in more seats and will keep looping if they say yes.

I thought that the spotstaken variable would be able to keep track of the number of seats taken, and then end the loop when it reached 28- the total number of seats... But it doesn't, and I really can't figure out why.

Please please help!


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
101
#include <iostream>
#include <string>
using namespace std;

int main ()
{ 
	int spotstaken = 0;
	int verticalindex;
	int acrossindex;
	int row;
	char seat;
	char again;

	string plane [7][4] = {{"1A","1B","1C","1D"}, //creates a 2 dimensional table array and sets a value to each spot on the table
						   {"2A","2B","2C","2D"}, //It's set up in a square like this just to make it easier to recognize what's what on the table- just more organized
						   {"3A","3B","3C","3D"},
						   {"4A","4B","4C","4D"},
						   {"5A","5B","5C","5D"},
						   {"6A","6B","6C","6D"},
						   {"7A","7B","7C","7D"}};
do
{
	for (int x = 0; x < 7; x++) //A for loop for displaying the table created above to the user. So long as x is less than 7 it will continue to display, same for y being less than 4.
	{
		for (int y = 0; y < 4; y++)
		{
		cout << plane [x][y] << " " ; //the for loops enable all the x and y variables created above to be outputted at once to the user
		}
	cout << endl;
	}

	cout << "Above is the seating chart to an airplane." << endl;
	cout << "Type in the row of the seat you want. (EX: 1)" << endl;
	cin >> row;
	
	verticalindex = row - 1;

		if (verticalindex > 6 || verticalindex < 0)
		{
			spotstaken = spotstaken;
			cout << "Sorry, you must have typed something wrong. Try again?" << endl;
			cout << "Type 'y' or 'Y' to continue, or 'n' or 'N' to quit." << endl;
			cin >> again;
		}

	cout << "Now type in the letter of the seat you want. (EX: A)" << endl;
	cin >> seat;

		if (seat == 'a' || seat == 'A')
			acrossindex = 0;
		else if (seat == 'b' || seat == 'B')
			acrossindex = 1;
		else if (seat == 'c' || seat == 'C')
			acrossindex = 2;
		else if (seat == 'd' || seat == 'D')
			acrossindex = 3;
		else
		{
			spotstaken = spotstaken;
			cout << "Sorry, you must have typed something wrong. Try again?" << endl;
			cout << "Type 'y' or 'Y' to continue, or 'n' or 'N' to quit." << endl;
			cin >> again;
		}
					if (plane [verticalindex][acrossindex] == "X" )
						{
							spotstaken = spotstaken;
							cout << "Sorry, that seat is already taken. Do you want to try again?" << endl;
							cout << "Type 'y' or 'Y' to continue, or 'n' or 'N' to quit." << endl;
							cin >> again;
						}
					else if (plane [verticalindex][acrossindex] != "X")
						{
							cout << "Okay, your seat will be filled in the chart." << endl;
							plane [verticalindex][acrossindex] = 'X';
							spotstaken = spotstaken + 1;
							cout << "Would you like to contine filling in seats?" << endl;
							cout << "Type 'y' or 'Y' to continue, or 'n' or 'N' to quit." << endl;
							cin >> again;
						}
					else if (spotstaken == 28)
						again == 'n';

} while (again == 'y' || again == 'Y');

if (spotstaken == 28) 
	{
		cout << "The plane is now full!" << endl;

for (int x = 0; x < 7; x++) 
{
	for (int y = 0; y < 4; y++)
	{
	cout << plane [x][y] << " " ; 
	}
cout << endl;
} 
}

	system ("pause");
	return 0;
}
Last edited on
1
2
3
           else if (spotstaken == 28)
		again == 'n';


In this line you are asking the compiler if it is equal too instead of assigning it. So instead of again == 'n'; make it again = 'n'; that way you assign it once you have cycled through all of the seats, not ask if it is previously assigned.
Last edited on
Thank you! But the problem still persists...

I used some cout statements to figure out that the spotstaken variable is counting correctly, but for some reason, when it reaches 28, the program will still loop. Any ideas?

I made some edits to the code, but it doesn't seem to be helping...

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
101
#include <iostream>
#include <string>
using namespace std;

int main ()
{ 
	int spotstaken = 0;
	int verticalindex;
	int acrossindex;
	int row;
	char seat;
	char again;

	string plane [7][4] = {{"1A","1B","1C","1D"}, //creates a 2 dimensional table array and sets a value to each spot on the table
						   {"2A","2B","2C","2D"}, //It's set up in a square like this just to make it easier to recognize what's what on the table- just more organized
						   {"3A","3B","3C","3D"},
						   {"4A","4B","4C","4D"},
						   {"5A","5B","5C","5D"},
						   {"6A","6B","6C","6D"},
						   {"7A","7B","7C","7D"}};
do
{
	for (int x = 0; x < 7; x++) //A for loop for displaying the table created above to the user. So long as x is less than 7 it will continue to display, same for y being less than 4.
	{
		for (int y = 0; y < 4; y++)
		{
		cout << plane [x][y] << " " ; //the for loops enable all the x and y variables created above to be outputted at once to the user
		}
	cout << endl;
	}

	cout << "Above is the seating chart to an airplane." << endl;
	cout << "Type in the row of the seat you want. (EX: 1)" << endl;
	cin >> row;
	
	verticalindex = row - 1;

		if (verticalindex > 6 || verticalindex < 0)
		{
			spotstaken = spotstaken;
			cout << "Sorry, you must have typed something wrong. Try again?" << endl;
			cout << "Type 'y' or 'Y' to continue, or 'n' or 'N' to quit." << endl;
			cin >> again;
		}

	cout << "Now type in the letter of the seat you want. (EX: A)" << endl;
	cin >> seat;

		if (seat == 'a' || seat == 'A')
			acrossindex = 0;
		else if (seat == 'b' || seat == 'B')
			acrossindex = 1;
		else if (seat == 'c' || seat == 'C')
			acrossindex = 2;
		else if (seat == 'd' || seat == 'D')
			acrossindex = 3;
		else
		{
			cout << "Sorry, you must have typed something wrong. Try again?" << endl;
			cout << "Type 'y' or 'Y' to continue, or 'n' or 'N' to quit." << endl;
			cin >> again;
		}
					if (plane [verticalindex][acrossindex] == "X")
						{
							cout << "Sorry, that seat is already taken. Do you want to try again?" << endl;
							cout << "Type 'y' or 'Y' to continue, or 'n' or 'N' to quit." << endl;
							cin >> again;
						}
					else if (plane [verticalindex][acrossindex] != "X")
						{
							cout << "Okay, your seat will be filled in the chart." << endl;
							plane [verticalindex][acrossindex] = 'X';
							spotstaken = spotstaken + 1;
							
							if (spotstaken < 28) //here is where the main edit I thought would help is...
							{
							cout << "Would you like to contine filling in seats?" << endl;
							cout << "Type 'y' or 'Y' to continue, or 'n' or 'N' to quit." << endl;
							cin >> again;
							}
						}

} while (again == 'y' || again == 'Y' && spotstaken != 28 ); 

if (spotstaken == 28) 
	{
		cout << "The plane is now full!" << endl;

for (int x = 0; x < 7; x++) 
{
	for (int y = 0; y < 4; y++)
	{
	cout << plane [x][y] << " " ; 
	}
cout << endl;
} 
}

	system ("pause");
	return 0;
}
I ended up adding in an else statement to the code, and it worked! Here's the final result!

Thanks so much for the help! It helped me figure out what I was missing!
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
101
102
103
#include <iostream>
#include <string>
using namespace std;

int main ()
{ 
	int spotstaken = 0;
	int verticalindex;
	int acrossindex;
	int row;
	char seat;
	char again;

	string plane [7][4] = {{"1A","1B","1C","1D"}, //creates a 2 dimensional table array and sets a value to each spot on the table
						   {"2A","2B","2C","2D"}, //It's set up in a square like this just to make it easier to recognize what's what on the table- just more organized
						   {"3A","3B","3C","3D"},
						   {"4A","4B","4C","4D"},
						   {"5A","5B","5C","5D"},
						   {"6A","6B","6C","6D"},
						   {"7A","7B","7C","7D"}};
do
{
	for (int x = 0; x < 7; x++) //A for loop for displaying the table created above to the user. So long as x is less than 7 it will continue to display, same for y being less than 4.
	{
		for (int y = 0; y < 4; y++)
		{
		cout << plane [x][y] << " " ; //the for loops enable all the x and y variables created above to be outputted at once to the user
		}
	cout << endl;
	}

	cout << "Above is the seating chart to an airplane." << endl;
	cout << "Type in the row of the seat you want. (EX: 1)" << endl;
	cin >> row;
	
	verticalindex = row - 1;

		if (verticalindex > 6 || verticalindex < 0)
		{
			spotstaken = spotstaken;
			cout << "Sorry, you must have typed something wrong. Try again?" << endl;
			cout << "Type 'y' or 'Y' to continue, or 'n' or 'N' to quit." << endl;
			cin >> again;
		}

	cout << "Now type in the letter of the seat you want. (EX: A)" << endl;
	cin >> seat;

		if (seat == 'a' || seat == 'A')
			acrossindex = 0;
		else if (seat == 'b' || seat == 'B')
			acrossindex = 1;
		else if (seat == 'c' || seat == 'C')
			acrossindex = 2;
		else if (seat == 'd' || seat == 'D')
			acrossindex = 3;
		else
		{
			cout << "Sorry, you must have typed something wrong. Try again?" << endl;
			cout << "Type 'y' or 'Y' to continue, or 'n' or 'N' to quit." << endl;
			cin >> again;
		}
					if (plane [verticalindex][acrossindex] == "X")
						{
							cout << "Sorry, that seat is already taken. Do you want to try again?" << endl;
							cout << "Type 'y' or 'Y' to continue, or 'n' or 'N' to quit." << endl;
							cin >> again;
						}
					else if (plane [verticalindex][acrossindex] != "X")
						{
							cout << "Okay, your seat will be filled in the chart." << endl;
							plane [verticalindex][acrossindex] = 'X';
							spotstaken = spotstaken + 1;
							
							if (spotstaken < 28)
							{
							cout << "Would you like to contine filling in seats?" << endl;
							cout << "Type 'y' or 'Y' to continue, or 'n' or 'N' to quit." << endl;
							cin >> again;
							}
							else //this is the added part of the code. Not sure why it makes a difference, but it works!
								again = 'n';
						}

} while (again == 'y' || again == 'Y' && spotstaken != 28 );

if (spotstaken == 28) 
	{
		cout << "The plane is now full!" << endl;

for (int x = 0; x < 7; x++) 
{
	for (int y = 0; y < 4; y++)
	{
	cout << plane [x][y] << " " ; 
	}
cout << endl;
} 
}

	system ("pause");
	return 0;
}
Last edited on
Topic archived. No new replies allowed.