2012-04-11 59 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的元素的數量保持不變,並使用該計數來中斷循環。

相關問題