2016-03-30 30 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

相关问题