2012-04-11 82 views
-2

我收到错误编译错误:之前预期的表达“{”令牌

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

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

+4

这听起来像功课。是吗? – unwind 2012-04-11 12:12:50

+0

它是每周实验室的一部分,我只是无法boathered描述的问题ahaha – user1304516 2012-04-11 12:15:42

+0

我不认为这是非常优雅的期待别人做更多的工作,因为你*不能boathered * – Yuri 2012-04-11 12:17:59

回答

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)); 
+0

嗯,好吧,我似乎得到了类似的结果,我第一次尝试,现在我唯一的问题是我如何让我的代码生成随机座位数:|。当我运行我的代码时,即使使用srand – user1304516 2012-04-11 12:28:01

+1

'x'和'y'将在while循环外获得固定值并且永远不会改变,每次都会生成相同的座位号。 – 2012-04-11 12:32:16

+0

为什么随机划分?分配下一个免费座位 – 2012-04-11 12:44:06

0

您使用以下检查所有的数组元素是1 :

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

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

相关问题