gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:03:01 25 4
gpt4 key购买 nike

当我编译代码时,程序下面的两个连接函数无法正常工作。当我在我的电脑上编译代码时,一旦加载其中一个板并返回主菜单,选项 1-2 就可以正常工作,但我无法使用第三个选项,但我没有收到任何错误。换句话说,我无法退出游戏。相反,它会打印“再见!”并要求我选择一个选项。仅当我进入“do while”循环并选择第三个选项时才会发生这种情况。当我在终端中编译代码时,我以某种方式收到标题中提供的错误消息。终端中也会出现同样的错误。有什么想法如何解决这个问题吗?如果需要,我可以提供额外的代码片段。

第一个:

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

关于纸板.c :12:1: warning: control reaches end of non-void function [-Wreturn-type],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com