compile error: expected expression before ‘{’ token

1k views Asked by At

I am getting the error

error: expected expression before ‘{’ token

when trying to compile the following code:

#include <stdio.h>

int main()
{
    srand (time(NULL));
    int Seat[10] = {0,0,0,0,0,0,0,0,0,0};
    int x = rand()%5;
    int y = rand()%10;

    int i, j;
    do {
        printf("What class would you like to sit in, first (1) or economy (2)?");
        scanf("%d", &j);
        if(j == 1){
            Seat[x] = 1;
            printf("your seat number is %d and it is type %d\n", x, j);
        }
        else{
            Seat[y] = 1;
            printf("your seat number is %d and is is type %d\n", y, j);
        }
    }while(Seat[10] != {1,1,1,1,1,1,1,1,1,1});
}

Background: The program is designed to be an airline seat reservation system.

2

There are 2 answers

4
Andreas Brinck On

The line:

 while(Seat[10] != {1,1,1,1,1,1,1,1,1,1});

is not valid C syntax. I would add a some variable like allOccupied and do the following:

bool allOccupied = false;
do
{
   ...
   //Check if all Seats are occupied and set allOccupied to true if they are
}
while (!allOccupied);

Another alternative would be to add something like:

int Full[10] = {1,1,1,1,1,1,1,1,1,1};
do
{
}
while(memcmp(Full, Seat, sizeof(Full));
0
codaddict On

You are using the following to check if all the array elements are 1:

while(Seat[10] != {1,1,1,1,1,1,1,1,1,1});

which is incorrect. You need to run a loop and check each element or a better way is to keep a count of elements that have been changed from 0 to 1 and use that count to break the loop.