开发者

Bug in an addition drill program

This is an addition drill program with a bug that I can't fix up. I can't input Y/N characters in the program, please help me to fix it up.

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

mai开发者_StackOverflow中文版n()
{
 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 ! */

Another problem should be scanf("%c"): there is a buffering-problem, you must clear the input buffer after that.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜