簡體   English   中英

附加鑽取程序中的錯誤

[英]Bug in an addition drill program

這是一個附加鑽取程序,帶有一個我無法修復的錯誤。 我無法在程序中輸入Y / N字符,請幫助我進行修復。

#include <conio.h>
#include <stdio.h>

main()
{
 int answer, count;
 int ch;
 ch = getche();

 for(count=1; count<11; count++) {
  printf("What is %d + %d? ", count, count);
  scanf("%d", &answer);
  if(answer == count + count) printf("Right!\n");
  else{
   printf("Sorry, you're wrong\n");
   printf("Would you like to try again? Y/N: \n");
   scanf("%c", &ch);

   if(ch=='Y') {
    printf("\nWhat is %d + %d? ", count, count);
    scanf("%d", &answer);
    if(answer == count+count) printf("Right!\n");
    else
     printf("Wrong, the answer is %d\n", count+count);
    }

   else
    printf("The answer is %d\n", count+count);
   }
  }
  return 0;
 }
scanf("%c", &ch); /* ch should be a char not an int ! */

另一個問題應該是scanf(“%c”):存在一個緩沖問題,您必須在此之后清除輸入緩沖區。

  else{ <<<< I think this might be the issue... Everything is within this else ... maybe try to minimize nesting?
   printf("Sorry, you're wrong\n"); 
   printf("Would you like to try again? Y/N: \n"); 
   scanf("%c", &ch); 

   if(ch=='Y') { 
    printf("\nWhat is %d + %d? ", count, count); 
    scanf("%d", &answer); 
    if(answer == count+count) printf("Right!\n"); 
    else 
     printf("Wrong, the answer is %d\n", count+count); 
    } 

   else 
    printf("The answer is %d\n", count+count); 
   } <<<<< I think this might be the issue... 

暫無
暫無

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

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