C语言scanf语句导致无法进入循环?

c小白Q
这个程序是要记录影片,用的结构体和malloc动态内存分配
星号中间的就是问题语句,如果没有这个scanf,程序可以正常进入循环
代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 50
#define RAT 5
struct movie{
char name[SIZE];
int rating;
};
void * s_gets(char *,int n);
int main()
{
int i=0,j,k;
struct movie *mov;
printf("输入你想录入的影片数量\n");
scanf("%d",&k);
mov=(struct movie *) malloc(k*sizeof(struct movie));
printf("输入你想录入的第一部影片名称\n");
while(i<k&&s_gets(mov[i].name,SIZE)!=NULL&&mov[i].name[0]!='\0')
{

printf("输入影片序号:");
scanf("%d",&mov[i++].rating);
while(getchar()!='\n')
continue;
puts("输入下一部影片名称:(empty line to stop):");
}
if(i==0)
printf("No data entered\n");
else
printf("Here is the movie list:\n");

for(j=0;j<i;j++)
{
printf("Movie: %s Rating: %d\n",mov[j].name,mov[j].rating);

}printf("Bye\n");
return 0;
}

void * s_gets(char *ch,int n)
{
char *ret_val;
char *find;
ret_val=fgets(ch,n,stdin);
if(ret_val)
{
find=strchr(ch,'\n');
if(find)
*find='\0';
else
while(getchar()!='\n')
continue;
}
return ret_val;
}

问题已经解决,还是比较简单的。。。一个小问题而已。是scanf函数的问题。

我先贴图好吧,我运行的结果。

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define SIZE 50

#define RAT 5

struct movie{

char name[SIZE];

int rating;

};

void * s_gets(char *,int n);

int main()

{

int i=0,j,k;

struct movie *mov;

printf("输入你想录入的影片数量\n");

scanf("%d",&k);

mov=(struct movie *) malloc(k*sizeof(struct movie));

printf("输入你想录入的第一部影片名称\n");

while(i<k&&s_gets(mov[i].name,SIZE)!=NULL&&mov[i].name[0]!='\0')

{


printf("输入影片序号:");

scanf("%d",&mov[i++].rating);

while(getchar()!='\n')

continue;

puts("输入下一部影片名称:(empty line to stop):");

}//printf("%s\n",mov[0].name);

if(i==0)

printf("No data entered\n");

else

printf("Here is the movie list:\n");


for(j=0;j<i;j++)

{

printf("Movie: %s Rating: %d\n",mov[j].name,mov[j].rating);


}printf("Bye\n");

return 0;

}


void * s_gets(char *ch,int n)

{

char *ret_val;

char *find;

fflush(stdin); //getchar();可选,最好用fflush函数;

ret_val=fgets(ch,n,stdin);

if(ret_val)

{

find=strchr(ch,'\n');

if(find)

*find='\0';

else

while(getchar()!='\n')

continue;

}

return ret_val;

}

然后贴我的源码,就多了一行代码而已,在你的s_get函数里的fgets函数前面加了个fflush函数,位置,我已经加粗标明出来了。。。问题也很简单,就是在第一个scanf函数的时候,你是按了回车的,所以第二次使用scanf的时候,fgets直接吃掉了回车,这是我们不需要的,所以在这个之前,我们必须先把stdin输入流里的回车,清理掉,使用fflush函数一次清理了stdin输入流,等stdin输入流干净了之后,再调用fgets就不会出错了,当然也可以使用getchar函数把回车吃掉,不过比较麻烦,我一般喜欢用fflush函数,反正输入前,那些脏东西,我们不关心,也不需要,而且万一有多个呢,getchar只能清理一个脏东西,而fflush函数,直接全部冲掉,来的简单方便。。。。

问题解决,请采纳!不懂再问。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-11-13
现象描述:在编写C语言程序时,需要使用scanf语句从键盘上输入一个整型的数时,当错误...造成死循环。 问题解决:可以在scanf语句后面加上getcha
第2个回答  2021-11-13
scanf("%d",&a); 应当键入数字,如果键入字母,scanf 语句执行失败,a当中未取得输入值,scanf函数返回0。 不一定是死循环,根程功编制的能有关。 有些情况,是由于输入流里的你键入的字母没有被清掉,后面的输入语句连续执行失败。可以用 fflus...
第3个回答  2021-12-02
最佳答案:问题也很简单,就是在第一个scanf函数的时候,你是按了回车的,所以第二次使用scanf的时候,fgets直接吃掉了回车,这是我们不需要...
第4个回答  2021-11-13
scanf("%d",&a); 应当键入数字,如果键入字母,scanf 语句执行失败,a当中未取得输入值,scanf函数返回0。

不一定是死循环,根程功编制的能有关。

有些情况,是由于输入流里的你键入的字母没有被清掉,后面的输入语句连续执行失败。可以用 fflush(stdin); 清除多余的字符。
相似回答