0

I have an array of structures:

typedef struct
{
	char name[20];
	int ID;
	int status;
} seat;

Then, basically, I'm trying to print out the list of empty seats in a reservation system.

void listEmpty(seat plane[MAXCAP]) {
	int i;
	for(i = 0; i < MAXCAP; i++) {
		if(plane[i].status == 1) {
			printf("Seat ID %d\n", plane[i].ID);
		}
	}
}
plan[i].status == 1

indicates that the seat is empty. However, when it prints out, it prints out some weird long integers instead of Seat ID 1 to Seat ID 12. May I know where I have gone wrong?

The correct output should be to display Seat ID 1 to Seat ID 12 instead of these weird long integers. Thanks!

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by Ab000dy_85
0

Ok, managed to solve it. Should have used a counter.

void listEmpty(seat plane[MAXCAP]) {
	int i;
	int count = 0;
	for(i = 0; i < MAXCAP; i++) {
		if(plane[i].status == 1) {
			count++;
			printf("Seat ID %d\n", count);
		}
	}
}
0

Actually, I was thinking, is there another way to assign values to the

int id

in struct so that I can directly call from it?.. I.e. plane.id and it'll return the corresponding ID.

0

you can do this if you like to have id similar to the counter

void initializ(seat plane[], int MAXCAP )
static int CurrentID = 1;
for(i = 0; i < MAXCAP; i++) {
    plane[i].ID = CurrentID++;
}
void initializ(seat plane[], int MAXCAP )
    for(i = 0; i < MAXCAP; i++) {
        plane[i].ID = i;
    }
}

both working, but the first one will keep track of the last assigned value, so if you want to keep calling that function then use the fist one

Edited by Ab000dy_85: n/a

0

by the way you have to initialize the ID before using the struct so you have a valid ID number

Edited by Ab000dy_85: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.