| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1108 人关注过本帖
标题:新手求助一个关于时间函数和scanf输入跳过的问题
只看楼主 加入收藏
张仪1997
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2020-3-23
结帖率:50%
收藏
已结贴  问题点数:13 回复次数:3 
新手求助一个关于时间函数和scanf输入跳过的问题
我看书自学C语言然后现在遇到了一个问题,想了半天都想不明白,请大家帮我看看
按着书上的步骤做一个关于考验记忆力的小游戏,大概就是显示一串数字几秒钟,然后数字消失不见,用户凭着记忆力把数字输入进去,对了继续玩,错了显示分数并选择要不要继续玩。
运行程序后,本该显示1秒的数字都没有显示,过了1秒就直接提示用户输入数字,还有输入会跳过,因为至少是两个数字,所以每输入一个数字后要按一下空格再输其他数字,但是输入了两个后最后那个问用户是否继续的scanf就被跳过了,我在输入数字的那个scanf后面加了getchar函数的
我检查了半天没发现有什么问题,我自己也想不明白...大家帮帮忙,谢谢!!

图片附件: 游客没有浏览图片的权限,请 登录注册
他不会显示需要输入的数字,最后询问那个也被跳过了
程序代码:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<time.h>
#include<stdbool.h>
int main(void)
{
  
  const unsigned int delay=1;
  char play_again='Y';
  int number=0;//user's input;
  int tries=0;//attempt
  int digits=0;// number of digits in sequence
  time_t seed=0;//seed value for random number sequence
  time_t wait_start=0;// stores current time
  bool correct=true;
  int score=0;
  int total_digits=0;
  int game_time=0;
  clock_t start_time=0;
  
  
  printf("\nWelcome to this game, i think you know"
           " the rules,so press enter to play:"
           "remember, number only display %us!",delay);
  scanf("%c",&play_again);
  
  
  do
  {
    //initialize game    
    correct = true;
    tries=0;
    digits=2;
    start_time=clock();
      
    //inner loop for game    
    while(correct)
    {
      ++tries;//a new attempt
      wait_start=clock();
    
      //generate a sequence of digits and display them
      srand(time(&seed));
      for(int i=1;i<=digits;++i)
        printf("%d ",rand()%10);//output a random digit
  
      // wait one second 从这开始一直到检查输入之前,我知道有问题但我找不出是什么问题...
      for(;clock()-wait_start<delay*CLOCKS_PER_SEC;);
      
      //overwrite the digit sequence
      printf("\r");
      for(int i=1;i<=digits;i++)
      printf("  ");
      if(tries==1)
      printf("\nEnter the sequence\n");
      else
      printf("\r");
          
      //input sequence and check it against the original
      srand(seed);//reinitialize sequence
      for(int i=1;i<=digits;i++)
      {
        scanf("%d",&number);
        getchar();
        if(number!=rand()%10)
        {
          correct = false;
          break;
        }
      }

      //on every third successful try, increase length          
      if(correct &&((tries%3)==0))
        ++digits;
            
      printf("%s\n",correct?"Correct!":"Wrong!");
    }
    
      //output the score when game finished
      score=10*(digits-((tries%3)==1));
      total_digits=digits*(((tries%3)==0)?3:tries%3);
      if(digits>2)
      total_digits+=3*((digits-1)*(digits-2)/2-1);
      game_time=(clock()-start_time)/CLOCKS_PER_SEC-tries*delay;
      if(total_digits>game_time)
      score+=10*(game_time-total_digits);
      printf("\n\nGame time was %ds,"
             " Your score is %d\n",game_time,score);
      fflush(stdin);

      //check if user required new game
      printf("\nDo you want to play again?(y/n):");        
      scanf("%c",&play_again);//这个scanf会被跳过
  }
    while(tolower(play_again)=='y');
  
  return 0;
}


[此贴子已经被作者于2020-4-2 17:59编辑过]

搜索更多相关主题的帖子: 数字 int scanf 输入 printf 
2020-04-02 17:51
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:13 
fflush(stdin); 是用于输出流的 stdin是输入流 行为不确定
所有 scanf("%c",....
%c前面加一个空格 都改成
scanf(" %c",

https://zh.
2020-04-02 18:06
张仪1997
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2020-3-23
收藏
得分:0 
回复 2楼 lin5161678
为什么要在%c前加空格啊,不是说没影响的吗?
还有fflush(stdin) 我之前查的时候说是和getchar差不多,是用来清除缓存的,输出流和输入流我不太清楚是什么..等我查一下,还是谢谢你回复
2020-04-02 18:12
张仪1997
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2020-3-23
收藏
得分:0 
回复 2楼 lin5161678
fflush是用来清除输入输出缓冲区的,stdin是标准输入流,stdout是标准输出流所以那个应该没问题...吧。有错误的话还请大佬指出,谢谢!
2020-04-02 18:19
快速回复:新手求助一个关于时间函数和scanf输入跳过的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020237 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved