Run a check within an array / check when all seats are reserved

This code creates array of rows and seats, in a grid. The user can reserve a seat which is then marked by an 'x'.

I need to check when all seats are reserved.

I really would like any hints or tips; or even some commentary on how to do this with arrays. I don't really understand how to check something within an array.


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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
 
#include<iostream>
#include <sstream>
#include <locale>

const int MAX_ROWS = 7, MAX_SEATS = 4;

void initialize_seating(char seating[][MAX_SEATS],
	int total_rows, int total_seats);

void display_seating(char seating[][MAX_SEATS],
	int total_rows, int total_seats);

void new_line();
//subtract from again loop //

using namespace std;

int main ()
{
	
	
	char seating[MAX_ROWS][MAX_SEATS];

	int row, i, j;
	char seat;
	char again; // ask if User would like to choose additional seats // 
	char happy; // check if happy with initial seat selection//
	initialize_seating(seating, MAX_ROWS, MAX_SEATS);
	display_seating(seating, MAX_ROWS, MAX_SEATS);

	do {

		do {
	do {
	cout << "Enter a Row (1-7) you wish to sit: \n>" ;
	cin >> row;
		while (cin.fail())
		{
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "Please enter a number. \n" ;
		}

		} while (row!=1&&row!=2&&row!=3&&row!=4&&row!=5&&row!=6&&row!=7);

			do {
	cout << "Now Enter the seat (A-D) you wish to sit: \n>";
	
	cin >> seat;
		if (islower(seat))
			seat = toupper(seat);

			} while (seat!='A'&&seat!='B'&&seat!='C'&&seat!='D');
	
	cout << "You chose " << row << seat << endl;
	cout << "Are you happy with your selection? \n>";
	cin >> happy;
	new_line();
	} while (happy != 'Y' && happy != 'y');


	i = row - 1;
	j = toupper(seat) - toupper('A');
	seating[i][j] = 'X';
	



	display_seating(seating, MAX_ROWS, MAX_SEATS);

		//if (i >= MAX_ROWS && j >= MAX_SEATS)
		if (seating[i][j] == 'X')
		{
		cout << "All seats are now booked.\n" ;
		cin.get();
		return 0;
		}

	cout << "Would you like to reserve another seat? \n>" ;
	cin >> again ;
	new_line();
	}while (again != 'N' && again != 'n');

	cin.get();
	///return 0;
}
void display_seating(char seating[][MAX_SEATS],
	int total_rows, int total_seats)
{
	
	for(int i = 0; i < total_rows; i++)
	{
		cout << i+1;
		for(int j = 0; j < total_seats; j++)
		{
			cout << seating[i][j];
			
		}
		cout << endl;
	}
}
void initialize_seating(char seating[][MAX_SEATS],
	int total_rows, int total_seats)
{
	for(int i = 0; i < total_rows; i++)
	{
		for(int j = 0; j < total_seats; j++)
		{
			seating[i][j] = static_cast<char>(toupper('A') + j);
		}
	}
	
}

void new_line()
{
	char symbol;
	do
		{

			cin.get(symbol);

		} while (symbol != '\n');
}
Line 32: I'm not fond of do loops nested three deep here. Better to put each loop in a function and let those functions continue until it has a valid value. It's hard to follow where your loops end.

Line 56: You probably want to check if the seat is taken before asking the user if they're happy with the seat.

With regard to checking if all seats are taken, the easiest way to do this is to create a bool function that scans the seat array.
1
2
3
4
5
6
7
8
9
bool seating_full (seating[][MAX_SEATS], int total_rows, int total_seats)
{  for(int i = 0; i < total_rows; i++)
    {    for(int j = 0; j < total_seats; j++)
	  {  if (seating[i][j] != 'X') 
                return false;  // This seat is not taken, so not full 
	}
    }
    return true;  // All seats occupied	
}




thanks for the reply;

in regards to putting the loops in a function, do you mean put what is within the do-while loops into a function, I'm not really following how that could work. (noob here)

I understand how you checked for the vacancy, which is brilliant, but I also need to run a check to see if all of it is reserved? Should I do a similar bool function? The intent of my initial question was to check if all of the seats were reserved.

Thanks for the help!



Here's an example of putting one loop in a function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int get_row ()
{   do 
    {  cout << "Enter a Row (1-7) you wish to sit: \n>" ;
	cin >> row;
	while (cin.fail())
	{  cin.clear();
	    cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}
   } while (row < 1 || row > 7);                
   return row;   // Return the row only when it is valid
}

//  In main.  Makes main much easier to read.
    row = get_row();    // By calling function, eliminates nested loops 


I also need to run a check to see if all of it is reserved?

That's what I provided you. If all the seats are taken, seating_full() returns true.
if seating_full() returns false, then at least one seat is available.

thanks
I'll put everything in functions now, looks good;


My final questoins to you::::

how can i use the bool function?

should I call it first? should i do something like this:

1
2
3
4
5

if (seating_full(seating, MAX_ROWS, MAX_SEATS))
        cout << "its full.";
else
       cout << "its vacant" ;


or should I keep the printing inside the function?


just quickly, what would you suggest I do about checking the vacancy of a single seat? I just tried to use a bool function, similar to what you did, to check if the seat was available or not, and then to respond with a cout. but i got confuesd :(
Last edited on
I would suggest calling seating_full() first. After all, you don't want to ask the user to select a seat if the plane is full.

As for checking if a seat is already assigned, you really don't need a bool function for that.
You already have the logic for that at line 73, however, your cout at line 75 is incorrect.
Line 73 checks if a specific seat is occupied, so you want to display something like the following at line 75:
73
74
75
76
    if (seating[i][j] == 'X')
    {  cout << "Sorry, that seat is occupied.  Please select another seat" << endl;
	//  return 0;  This would cause the program to exit.  You want to loop back and ask again
    }




sorry if anyone read this before edit, i made a stupid mistake.

Last edited on
Topic archived. No new replies allowed.