添加钻孔程序中的错误

时间:2010-08-13 18:26:42

标签: c

这是一个额外的钻取计划,其中包含一些我无法修复的错误。我无法在程序中输入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;
 }

2 个答案:

答案 0 :(得分:1)

scanf("%c", &ch); /* ch should be a char not an int ! */

另一个问题应该是scanf(“%c”):存在缓冲问题,之后必须清除输入缓冲区。

答案 1 :(得分:-1)

  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...