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!

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);
		}
	}
}

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.

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 4 Years Ago by Ab000dy_85: n/a

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

Edited 4 Years Ago by Ab000dy_85: n/a

This article has been dead for over six months. Start a new discussion instead.