gets函数为什么不停下来等待我输入

clleady 2011-07-13 05:20:14
#include "stdio.h"
#include "conio.h"

main()
{
int n,j,i=0;
char str[30];
int count[10]={0};
printf("Start\r\n");

while(n!=-1)
{ scanf("%d",&n);
count[i++]=n;
}
while(1)
{
i=0;
j=0;
printf("\r\nOriginal input:");
printf("%s",&str);
printf("Rearranged line:");
while(count[j]!=-1)
{
for(i=count[j];i<=count[j+1];i++)
printf("%c",str[i]);
j=j+2;
}
}
getch();
}
第一次到gets函数有问题,它没有等待我输入。而之后通过while循环后,到gets函数都是正常的,会停下来,让我输入字符串。为什么第一次不停呢?谢谢高手指点。
...全文
316 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sbqjhipr 2011-07-13
  • 打赏
  • 举报
回复
你应该改一下循还的条件为你输入的某个字符就结束
sbqjhipr 2011-07-13
  • 打赏
  • 举报
回复
因为死循还了 while(1)
cbdxl 2011-07-13
  • 打赏
  • 举报
回复
6楼正解,因为前面用了scanf
SolarJupiter 2011-07-13
  • 打赏
  • 举报
回复
把 回车赋予了本该让你输入的那个字符的空间了。
ljhhh0123 2011-07-13
  • 打赏
  • 举报
回复
再加个getch()吧。你的问题跟这个程序一样:
#include "stdio.h"
main()
{
int n;
scanf("%d",&n);
getchar();
}

getchar得不到输入,因为刚才输入整数时你输入了回车,scanf只取了整数,而输入流里还有个换行符'\n',所以getchar()得到的是那个换行符。
yuqangy 2011-07-13
  • 打赏
  • 举报
回复
是有这种问题,我也碰过,getch不停下来等待输入,程序直接return
至善者善之敌 2011-07-13
  • 打赏
  • 举报
回复
gets ?????????
luciferisnotsatan 2011-07-13
  • 打赏
  • 举报
回复
gets函数在哪里???
atzai 2011-07-13
  • 打赏
  • 举报
回复
没看到你代码里有第一次的gets
lyzzzwx 2011-07-13
  • 打赏
  • 举报
回复
gets()->getch()?

gets()少用,函数有问题.

70,029

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧