C语言程序中gets()怎老是在执行时自动跳过不接受输入?
在VC下调此程序时出现该问题...程序目的是输入三本书的信息.#include<stdio.h>#include<dos.h>main(){structbnotes{ch...
在VC下调此程序时出现该问题...程序目的是输入三本书的信息.
#include<stdio.h>
#include<dos.h>
main()
{struct bnotes
{char bn[100];
char an[100];
int da_year;
int da_day;
int da_mon;
int sta;
}book[3];
int i;
system("cls");
for(i=0;i<3;i++)
{printf("\nInput the name of book%d:\n",i+1);
gets(book[i].bn);
printf("\nInput the author's name of book%d:\n",i+1);
gets(book[i].an);
printf("\nNow input the pressing date of the book:(yyyy-mm-dd)\n");
scanf("%d-%d-%d",&book[i].da_year,&book[i].da_mon,&book[i].da_day);
printf("\nFinally is the book borrowed?(Input 0 for no,others for yes)\n");
book[i].sta=getch();
}
for(i=0;i<3;i++)
{printf("\nThe book%d's name is %s,the author's name is %s,the pressing date is %d-%d-%d.\n",i+1,book[i].bn,book[i].an,book[i].da_year,book[i].da_mon,book[i].da_day);
if(book[i].sta=='0') printf("The book is not borrowed yet.\n");
else printf("The book is already borrowed.\n");
system("pause");
}
}
代码编译正常,但是从输入第二本书的信息时开始第一个gets()不接受输入,郁闷...... 展开
#include<stdio.h>
#include<dos.h>
main()
{struct bnotes
{char bn[100];
char an[100];
int da_year;
int da_day;
int da_mon;
int sta;
}book[3];
int i;
system("cls");
for(i=0;i<3;i++)
{printf("\nInput the name of book%d:\n",i+1);
gets(book[i].bn);
printf("\nInput the author's name of book%d:\n",i+1);
gets(book[i].an);
printf("\nNow input the pressing date of the book:(yyyy-mm-dd)\n");
scanf("%d-%d-%d",&book[i].da_year,&book[i].da_mon,&book[i].da_day);
printf("\nFinally is the book borrowed?(Input 0 for no,others for yes)\n");
book[i].sta=getch();
}
for(i=0;i<3;i++)
{printf("\nThe book%d's name is %s,the author's name is %s,the pressing date is %d-%d-%d.\n",i+1,book[i].bn,book[i].an,book[i].da_year,book[i].da_mon,book[i].da_day);
if(book[i].sta=='0') printf("The book is not borrowed yet.\n");
else printf("The book is already borrowed.\n");
system("pause");
}
}
代码编译正常,但是从输入第二本书的信息时开始第一个gets()不接受输入,郁闷...... 展开
3个回答
展开全部
这是一个字符串回车键重复读取问题,第一个字符串接收以后,可能在键盘的缓冲区里面还保存一个回车换行之类的键符。
解决办法是:
在新的读取之前,先输出一行键盘输入提示符。在提示符之前,在添加一句键盘缓冲区清除语句。清除键盘缓冲区也可以使用stdio.h中的标准函数完成,使用方法如下:
==================================
fflush(stdin) 或者: flushall()
对程序做如下修改:
==============================================
printf("\nInput the name of book%d:\n",i+1);
gets(book[i].bn);
printf("\nInput the author's name of book%d:\n",i+1);
gets(book[i].an);
改为:
============================
printf("\nInput the name of book%d:\n",i+1);
gets(book[i].bn);
flushall(); /* 用来去掉缓冲区中多余的回车换行符 */
printf("\nInput the author's name of book%d:\n",i+1);
gets(book[i].an);
=========================
一定可以解决的,试试吧。
该加分了吧?
解决办法是:
在新的读取之前,先输出一行键盘输入提示符。在提示符之前,在添加一句键盘缓冲区清除语句。清除键盘缓冲区也可以使用stdio.h中的标准函数完成,使用方法如下:
==================================
fflush(stdin) 或者: flushall()
对程序做如下修改:
==============================================
printf("\nInput the name of book%d:\n",i+1);
gets(book[i].bn);
printf("\nInput the author's name of book%d:\n",i+1);
gets(book[i].an);
改为:
============================
printf("\nInput the name of book%d:\n",i+1);
gets(book[i].bn);
flushall(); /* 用来去掉缓冲区中多余的回车换行符 */
printf("\nInput the author's name of book%d:\n",i+1);
gets(book[i].an);
=========================
一定可以解决的,试试吧。
该加分了吧?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询