编译错误:'{'标记之前的预期表达式

时间:2012-04-11 12:11:48

标签: c compiler-errors

我收到错误

error: expected expression before ‘{’ token

尝试编译以下代码时:

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

背景:该计划旨在成为航空公司座位预订系统。

2 个答案:

答案 0 :(得分:4)

该行:

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

是无效的C语法。我会添加一些像allOccupied这样的变量并执行以下操作:

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

另一种选择是添加类似:

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

答案 1 :(得分:0)

您正在使用以下内容检查所有数组元素是否为1:

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

这是不正确的。您需要运行循环并检查每个元素,或者更好的方法是保持已从0更改为1的元素数,并使用该计数来打破循环。