-3

我收到错误

错误:“{”标记之前的预期表达式

尝试编译以下代码时:

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

背景:该程序旨在成为一个航空公司座位预订系统。

4

2 回答 2

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));
于 2012-04-11T12:15:49.070 回答
0

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

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

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

于 2012-04-11T12:16:43.553 回答