当前位置: 代码迷 >> C语言 >> [求助]请问这个程序怎么调试的时候第二个SCANF执行不了.
  详细解决方案

[求助]请问这个程序怎么调试的时候第二个SCANF执行不了.

热度:418   发布时间:2007-06-12 19:49:13.0
[求助]请问这个程序怎么调试的时候第二个SCANF执行不了.
各位高手请帮我看一下下面这个程序,怎么调试的时候第二个SCANF执行不了.
而且rand()这个函数,每次随机产生的数值第一个一定是41,为什么啊.?
#include<stdio.h>
#include<stdlib.h>
void main(void)
{
char i;char j;
int m,n;
m=rand()%100;
printf("*********************\n*WELCOME TO THE GAME*\n*********************\n");
printf("do you want to play this game?(Y/N)\n");
scanf("%c",&j);
if(j=='y'||j=='Y')
{
printf("do you want to see the help file?(Y/N)\n");
scanf("%c",&i);
if(i=='y'&&i=='Y')
{printf("***The computer will randomly produce a number between 0-100 that you won't know.");
printf("Your job is following the prompt and finding out the wright number***.\n");
}
else
printf("LET'S GO!\n");

while((m-1)!=0)
{
printf("Now plesase input the number you guess:\n");
scanf("%d",&n);
if(n==m)
{printf("YEAH!That's right!\n");
break;}
else if(n<m)
{printf("The number you put in is too small,please try again:\n");
continue;}
else if(n>m)
{printf("The number you put in is too big,please try again:\n");
continue;}
}
system("pause");
}
else
printf("thanks,see you later!");
}
执行情况如下图所示:

请高手指点程序哪里有问题,谢谢了..
搜索更多相关的解决方案: SCANF  rand  调试  

----------------解决方案--------------------------------------------------------
小弟 才学。。
只能表达自己的看法

可能是没有
fflush(stdin);

这个吧

%c 以后最好清空一下。

这样比较好哦~
----------------解决方案--------------------------------------------------------

我也是刚学的....
嗯,加上这个后程序运行正常,也就是说如果没有清空的话,默认的是键入字符为上一次所键入的.?
而且每次那个函数随机产生器所产生的函数都是一样的.请问一下怎么解决这个问题.?


----------------解决方案--------------------------------------------------------

没有好心人回答一下嘛.?


----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
void main(void)
{
char i;char j;
int m,n;
m=rand()%100;
printf("*********************\n*WELCOME TO THE GAME*\n*********************\n");
printf("do you want to play this game?(Y/N)\n");
scanf("%c",&j);
getchar();
if(j=='y'||j=='Y')
{
printf("do you want to see the help file?(Y/N)\n");
scanf("%c",&i);
getchar();
if(i=='y'||i=='Y')
{printf("***The computer will randomly produce a number between 0-100 that you won't know.");
printf("Your job is following the prompt and finding out the wright number***.\n");
}
else
printf("LET'S GO!\n");

while((m-1)!=0)
{
printf("Now plesase input the number you guess:\n");
scanf("%d",&n);
if(n==m)
{printf("YEAH!That's right!\n");
break;}
else if(n<m)
{printf("The number you put in is too small,please try again:\n");
continue;}
else if(n>m)
{printf("The number you put in is too big,please try again:\n");
continue;}
}
system("pause");
}
else
printf("thanks,see you later!");
}

----------------解决方案--------------------------------------------------------
rand函数需要改变种子数,否则的话出来的数永远是一样的。
----------------解决方案--------------------------------------------------------

嗯..楼上说的应该是对的,请问一下怎么改变种子数.?


----------------解决方案--------------------------------------------------------
srand((int)time(0));
这个解决了种子的问题.谢谢各位了..
----------------解决方案--------------------------------------------------------
  相关解决方案