scanf()函数被跳过不运行的原因

       当你进行过了scanf函数后, 在第二次scanf函数时, 由于缓存区里依然存着第一次结束时送入的回车符,在第二次时没有读入就直接把回车符送入了,所以没有真正起到读入数据的作用.只要在合适的地方加入fflush(stdin)清空缓存区,再运行scanf时就可以了.

#include<math.h>
#include <stdlib.h>
#include<stdio.h>


char judge(int try,int answer){

   if(try==answer){

      return 'e';
   }
   else return 'u';

 

 

void main(){

   int order=0,get=1;
   int try=0;
   int level;
   int bits;
   int answer;
   char result='n';
   int length;
   char judgeIf;

   printf("Game begins!/nWelcome to the number guessing game!/nThis is my first biggest program./nHope you like it./nIt is edited by c-resource./n");
   printf("Now it begins./n");

   do{

      printf("Choose the level 3-10 :");
      scanf("%d",&level);

      printf("Set the length of the number 3-5 bits :");
      scanf("%d",&bits);
  
      answer=random(bits);

      printf("Now the number is ready.You can try it now./nInput your try :");

      while(order<level){
         printf("/nTime %d : (%d)",++order,answer);
         scanf("%d",&try);

 

         if(judge(try,answer)=='e'){

            printf("/nYou are right.Congratulations!");
            break;
           
         }
         else {
            if(order==level){

               printf("You are failed.");
            }
            else printf("Try it again.");
         }
      }
  

  
      if(get==1){
         printf("Are you want to play it again?");
         scanf("%c",&result);
         printf("/nYou are so clever now with the help of my software.");
      }

   }
   while(result=='y');
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值