2014-10-07 64 views
-3

我有一些問題,當我嘗試編譯我很簡單的C遊戲..C,GCC編譯錯誤簡單的程序

它說:

「行125錯誤:預期標識符或‘(’前'返回」 返回0;」

「線126錯誤:預期標識符或 '(' 前 '}' 令牌 }」

(行125和126都在我的代碼中的最後2行)

<#include <stdio.h> 
    #include <time.h> 
    #include <stdlib.h> 



    int main (void) 

    { 

     int headbang,x,y,fx,fy; 
     char choice; 

     headbang = 0; 



    { 
    srand(1); 
    int fx = rand()%5 + 1; 
    } 
    { 
    srand(2); 
    int fy = rand()%5 + 1; 
    } 
    { 
    srand(3); 
    int x= rand()%5 + 1; 
    } 
    { 
    srand(4); 
    int y= rand()%5 + 1; 
    } 


     printf ("It is pitch black. You have no idea how you got here, but you \nknow that you have to find some source of light in order to get \nout. \n \n"); 



     printf ("Where do you want to go? "); 


     { 
     scanf(" %c", &choice); 


     switch (choice) 
    { 
     case 'n': 
    x++; 

    printf ("You crawl North.\n"); 
    break; 



     case 's': 
    x--; 

    printf("You crawl South \n"); 


     case 'e': 

    y++; 

    printf ("You crawl East.\n"); 
    break; 

     case 'w': 


    y--; 

    printf ("You crawl West.\n"); 
    break; 

    default: 
    printf ("Sorry, you can only go north, south, east or west."); 


    } 
     if (x==0 || y==0 || x==6 || y==6) 
    { 
    printf("Ouch! You hit your head in the wall."); 
    headbang++; 

     if (x==0) 
    { 
    x++; 
    } 

     else if (y==0) 
    { 
    y++; 
    } 

     else if (x==6) 
    { 
    x--; 
    } 

     else if (y==6) 
    { 
    y--; 
    } 

    } 

     if (headbang == 2) 
    { 
    printf ("You slip into a deep sleep, never to wake up again..."); 
    } 

     if (x==fx && y==fy) 
    { 
    printf("You feel something on the floor. It is a flashlight! \n You wake up, finding yourself in your own bed. What a nightmare!"); 
    break; 
    } 


    while (!(x==fx && y==fy)); 
    } 
    } 
     return 0; 
    } 
+3

這裏有一大筆小費:永遠不要在編譯之前編寫這麼多的代碼。學習編寫一段代碼,然後在繼續之前對其進行測試。 – Dan 2014-10-07 21:04:31

回答

3

你應該把返回之前的倒數第二}和刪除最後一個支架

+4

有一個upvote發現,在這個非常格式化的職位;) – MrHug 2014-10-07 21:01:31

+0

+1。我試圖修復它並放棄了。 – admdrew 2014-10-07 21:06:37