2016-03-30 32 views
0

當我編譯我的代碼時,程序下面的兩個連接函數無法正常工作。當我在我的電腦上編譯代碼時,一旦我加載其中一個電路板並返回到主菜單選項1-2可以正常工作,但我無法使用第三個選項,我還沒有收到任何錯誤。換句話說,我不能退出遊戲。相反,它會打印「再見!」並要求我選擇一個選項。只有當我進入'do while'循環並在那裏選擇第三個選項時纔會發生這種情況。當我在終端中編譯代碼時,我會以某種方式獲得標題中提供的錯誤消息。終端發生同樣的錯誤。任何想法如何解決這個問題?如果需要,我可以提供額外的代碼片段。Carboard.c:12:1:警告:控制達到非無效功能的結束[-Wreturn-type]

第一招:

int main() { 

printf("Welcome to Car Board \n"); 
printf("-------------------- \n"); 
printf("1. Play game \n"); 
printf("2. Show student's information \n"); 
printf("3. Quit \n\n"); 

showMenu(); 

} 

void showMenu(){ 
    Cell board[BOARD_HEIGHT][BOARD_WIDTH]; 
    int choice = validateNumber(); 
    if(choice == 1){ 

    showCommands(); 
    initialiseBoard(board); 
    displayBoard(board, NULL); 

    printf("load <g>\n"); 
    printf("quit\n\n"); 

    playGame(); 

    } 

    if (choice == 2){ 

    showStudentInformation(); 

    } 

    if (choice == 3){ 

    printf("Good Bye!\n\n"); 

} 

    else showMenu(); 
} 

第二個:

void playGame() 
{ 
    Cell board[BOARD_HEIGHT][BOARD_WIDTH]; 
    char str1[] = {"load 1"}; 
    char str2[] = {"load 2"}; 
    char str3[] = {"quit"}; 
    char * choice; 


do { 
    choice = validateString(); 
    if (strcmp(choice, str1) == 0) { 

     printf("\n"); 
     loadBoard(board, BOARD_1); 
     displayBoard(board, NULL); 
     playGame(); 

    } 

    if(strcmp(choice, str2) == 0){ 

     printf("\n"); 
     loadBoard(board, BOARD_2); 
     displayBoard(board, NULL); 
     playGame(); 

    } 

    if(strcmp(choice, str3) == 0){ 

     printf("\n"); 
     printf("Welcome to Car Board \n"); 
     printf("-------------------- \n"); 
     printf("1. Play game \n"); 
     printf("2. Show student's information \n"); 
     printf("3. Quit \n\n"); 
     showMenu(); 

    } 

    else { 
     printf("Invalid input\n\n"); 
     playGame(); 

    } 

} 
while(strcmp(choice, str1) != 0 && strcmp(choice, str2) != 0 && strcmp(choice, str3) != 0); 


} 
+0

嘗試增加在主要的最終回'0'擺脫警告 – 4386427

+0

顯示爲'validateNumber' – 4386427

+0

代碼'詮釋validateNumber(){ 字符[LINE_LEN + EXTRA_SPACES]; char * end; int input; do { printf(「請輸入您的選擇:」); fgets(line LINE_LEN + EXTRA_SPACES,stdin); if(line [strlen(line) - 1]!='\ n'){ readRestOfLine(); 繼續; } line [strlen(line) - 1] = 0; input = strtol(line,&end,0); } while(* end); 返回輸入; }' – sscryp

回答

0

嘗試在主末尾添加返回0擺脫警告

而且你應該擺脫一切遞歸調用。取而代之的

void showMenu(){ 
    Cell board[BOARD_HEIGHT][BOARD_WIDTH]; 
    int choice = validateNumber(); 
    if(choice == 1){ 
     showCommands(); 
     initialiseBoard(board); 
     displayBoard(board, NULL); 
     printf("load <g>\n"); 
     printf("quit\n\n"); 
     playGame(); 
    } 

    if (choice == 2){ 
     showStudentInformation(); 
    } 

    if (choice == 3){ 
     printf("Good Bye!\n\n"); 
    } 
    else showMenu(); // Recursive call 
} 

嘗試

void showMenu(){ 
    Cell board[BOARD_HEIGHT][BOARD_WIDTH]; 

    while(1) { // Loop until choice is 3 

     int choice = validateNumber(); 
     if(choice == 1){ 
      showCommands(); 
      initialiseBoard(board); 
      displayBoard(board, NULL); 
      printf("load <g>\n"); 
      printf("quit\n\n"); 
      playGame(); 
     } 

     else if (choice == 2){ 
      showStudentInformation(); 
     } 

     else if (choice == 3){ 
      printf("Good Bye!\n\n"); 

      return; // End the function 
     } 
    } 
} 

同樣的想法應該被應用到PlayGame功能擺脫遞歸調用。另外,不要從PlayGame調用ShowMenu - 只是做一個return

+0

非常感謝!解決了我的問題你可以猜到,我對C編程語言相當陌生。試圖完成我的任務。我可以在未來使用更多的幫助!再次感謝! – sscryp

相關問題