簡體   English   中英

Carboard.c:12:1:警告:控制權到達非空函數的結尾[-Wreturn-type]

[英]Carboard.c:12:1: warning: control reaches end of non-void function [-Wreturn-type]

當我編譯代碼時,程序下面的兩個連接功能無法正常工作。 當我在PC上編譯代碼時,一旦加載了一塊板並返回主菜單,選項1-2即可正常工作,但我不能使用第三個選項。 換句話說,我不能退出游戲。 而是打印“再見!” 並要求我選擇一個選項。 僅當我進入“ do while”循環並在那里選擇第三個選項時,才會發生這種情況。 當我在Terminal中編譯代碼時,會以某種方式收到標題中提供的錯誤消息。 終端中發生相同的錯誤。 任何想法如何解決這個問題? 如果需要,我可以提供其他代碼段。

第一:

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


}

嘗試在main的末尾添加return 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM