check function for a 2d array.

I am trying to create a function that checks the available seating arrangement in a airline reservation program. The problem I am having is I am somehow referencing my array parameter wrong and I am not sure what to do to fix it. Any help would be great. The function I am referring to is the seat_availability() function.

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
//Airplane seat assignment

#include <iostream>
#include <cmath>

const int MAX_ROW = 8;//this translates to 7 since row-1 is used for the printing of the table
const int MAX_SEAT = 4;

void display_seating(char a[][MAX_SEAT], int rows);
void seat_availability(char a[][MAX_SEAT], int rows);

bool assign_seat(char a[][MAX_SEAT], int rows, int row, int col);

int main()
{
using namespace std;

int row, col;
int input_row;

char input_seat;
char seat[MAX_ROW][MAX_SEAT], ans;

for(row = 0; row < MAX_ROW; row++)
for(col = 0; col < MAX_SEAT; col++)seat[row][col] = static_cast<char>(toupper('A') + col);

display_seating(seat,MAX_ROW);

cout << "\nWelcome to the airline seating reservation system!\n" 
		 << "Please choose a row  and seat that you would like to "
		 << "\nsit in from the above seating chart.";
do
{
	seat_availability(seat,MAX_SEAT);

	cout << " \n\nEnter 1-7 for the row and "
	<< "\neither 'A', 'B', 'C', or 'D' for the seat selection: ";
	cin >> input_row >> input_seat;

	col = toupper(input_seat) - toupper('A');
	input_seat = col;

	// Check to see if seat is taken
	if (seat[input_row-1][col] == 'X')
	cout << "\nSorry, " << input_row << static_cast<char>(toupper('A') + col) << " is already taken.\n" << endl;
	else
	{
	cout << "\nOK, you've got " << input_row << static_cast<char>(toupper('A') + col) << endl << endl;
	seat[input_row-1][col]='X';

	}

	//now input an 'X' for the seat taken
	display_seating(seat,MAX_ROW);	

	cout << "\nWould you like to enter another reservation?";
	cin >> ans;
	}while (ans == 'Y' || ans == 'y');

return 0;

}

void display_seating(char a[][MAX_SEAT], int rows)

{
using namespace std;

for(int row = 1; row < rows; row++)
{
cout << "\t" << "\t" <<row;

for(int col = 0; col < MAX_SEAT; col++)
{
//I subtracted 1 from row to match row to the index
cout << a[row-1][col];
}
cout << endl;
}

}

void seat_availability(char a[][MAX_SEAT], int rows)

{
using namespace std;
for(int row = 1; row < rows; row++)
for(int col = 0; col < MAX_SEAT; col++);

// if my target array does not have an 'X' in it
if (a[][MAX_SEAT] != 'X')
{
	// do nothing
	;
}else
	cout << "The plane is full please try a different airline";
}


Thank you!
Last edited on
well from what I can see I have a few issues with whats goin on here. I could b wrong but this is just my opinion. I'm just starting out with c++ myself so i am by far not a programming genius or anything. First off not sure about this but, If you declare a variable in a prototype function declaration is that legal? that may b why your having a problem bc there is no line that just says "char a[];" If it is legal than it can't be a good practice. Anyway, you have using namespace std all over the place. Why not just declare it once globally? just seems like a waste of typing. an last but not least where you have you seat_availability function call in the do loop seems like odd placement bc the first time through its gona have an empty argument, dnt know if that could potentially lead to some kind of error or potential security hole lol but just seems to make more sense to run it after the proper info has been input. Ok well goodluck
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void seat_availability(char a[][MAX_SEAT], int rows)

{
using namespace std;
for(int row = 1; row < rows; row++)      //these two loops don't do anything
for(int col = 0; col < MAX_SEAT; col++);

// if my target array does not have an 'X' in it
if (a[/*you need to put something in here to index the array*/][MAX_SEAT] != 'X')
{
	// do nothing
	;   // why do you have a condition that does nothing?
}else
	cout << "The plane is full please try a different airline";
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// i'm guessing what you want is something like this:

void seat_availability(char a[][MAX_SEAT], int rows)
{
   using namespace std;
   for(int row = 1; row < rows; row++)
   {
      for(int col = 0; col < MAX_SEAT; col++)
      {
            // if my target array does not have an 'X' in it
            if (a[row][col] == 'X')  // might need to switch row and col, i didn't look that closely
               cout << "The plane is full please try a different airline";
      }
   }
}


edit: i didn't look very closely at the problem you're trying to solve, I'm just trying to illustrate, why you loops do nothing and a better way to handle that if/else block
Last edited on
@marky Thank you for the reply. I realize I can use 'using namespace std;' globally but for the scope of this college class we are to use it properly and this is proper according to the textbook we are using so my professor requires it. I am learning myself obviously so someone correct me if I am wrong but the declarations for the void function are perfectly legal and are well within the norm. Look at line 10 to see the declaration of the array. Lastly, the seating availability needs to be inside the loop so that at the start of every iteration, unless the user chooses to quit making reservations, it will check to make sure there is space on the plane to make a reservation, if it is outside the loop there is only one check and that is at the start of the program, so every time a user inputs a reservation there is no way of keeping track of the total seats available aside from looking at the updated display.

@phrophet Thank you also for the help. I ended up just doing the following and it worked albeit in a very robust and cranky manner. I will learn more error checking and tighten my security soon enough as this is veery weak imo.

1
2
3
4
5
6
7
8
9
10
11
12
void seat_availability(char a[][MAX_SEAT], int rows)
{
   using namespace std;
   for(int row = 1; row < rows; row++)
   for(int col = 0; col < MAX_SEAT; col++)
      
            // if my target array does not have an 'X' in it
            if (a[row+6][col] == 'X') //Stupid index throws me off here but this is kinda what I wanted..
           		cout << "The plane is full please try a different airline";
     
   
}


The assignment is submitted so I will mark this as resolved but I would like some more input if someone has anything else to add to help me understand functions and array parameters. thank you!

Warmaster
this is a simple example of what I belive you did. Its not the fact that you declared arguments in a prototype function. Its the fact that one of the variables in the arguement was never declared.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

void pissin(int a);


main()
{a=4;

	pissin(a);

}


void pissin(int a)


I believe this is what you did in short and when I entered this in it gave me an error a undefined.

like I said i don't see the line "char a[];" anywhere
Well char a[] is an array parameter kinda like a call/pass-by-reference parameter in a void function. It is associated with the array 'seat'. It is declared properly until you show me why it isn't. And who knows, that may very well be why the function I was working with didn't work, but it is one of the things we were learning this week and I am not extremely comfortable with it yet.

Warmaster
Your first array index is out of bounds when rows - row < 6
Last edited on
I had to finish this off bc i needed to learn what the deal was with this. And i know this was marked as solved but kinda not you said it still wasn't runnin right or w-e. this is what I did.

changed this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void seat_availability(char a[][MAX_SEAT], int rows)

{
using namespace std;
for(int row = 1; row < rows; row++)
for(int col = 0; col < MAX_SEAT; col++);

// if my target array does not have an 'X' in it
if (a[][MAX_SEAT] != 'X')
{
	// do nothing
	;
}else
	cout << "The plane is full please try a different airline";



to this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void seat_availability(char a[][MAX_SEAT], int rows)

{
using namespace std;
for(int row = 1; row < rows; row++)
for(int col = 0; col < MAX_SEAT; col++)

// if my target array does not have an 'X' in it
if (a[row-1][MAX_SEAT] != 'X')
{
	// do nothing
	;
}else
	cout << "The plane is full please try a different airline";
}



just subtracted a stray ; and added an expression to char a[]........ works like a champ.
To test that did you reserve all 28 seats of the plane and check to see at what point it indicated the plane was full? I tried that same variation and it was row 3 or 4 that is started indicating the plane was full.
no i did not run a complete test on it. I just got it running an did a few tests an it seemed to be spot on so didn't proceed farther. ill check it out. ya its exiting if seat is taken.

and god help you if you just enter some kinda unexpected result like a instead of 1a
Last edited on
Topic archived. No new replies allowed.