【问题标题】:Why gets() function skips when preceded by scanf("%d")?为什么 get() 函数在 scanf("%d") 前面时会跳过?
【发布时间】:2013-10-17 10:19:11
【问题描述】:

我想制作一个程序,以 Roll No 和 Full name 作为输入并简单地显示它 我的代码是 .此代码通过获取函数跳过扫描 n 的值。为什么会出现这个错误以及如何克服这个错误?

 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int r;
 char n[30];
 printf("enter your roll no");
 scanf("%d",&r);
 printf("enter your full name");
 gets(n);
 printf("roll no is %d ",r);
 printf("name is %s ",n);
 getch();
 }

当下面的代码扫描第一个获取值并跳过第二个时。

#include<stdio.h>
#include<conio.h>
void main()
{
 int r;
 char n[30], f[30];
 printf("enter your roll no");
 scanf("%d",&r);
 printf("enter your full name");
 gets(n);
 printf("enter your full name of your father ");
 gets(f);
 printf("roll no is %d ",r);
 printf("name is %s ",n);
 printf("father name is %s ",f);
 getch();
 }

【问题讨论】:

  • 使用fgets 而不是gets
  • 你也可以使用scanf("%s", r)。注意溢出:)
  • 欢迎来到 Stack Overflow。请记住,在发布问题时,必须实际发布某种问题。您发布的是一份声明和一些代码,但您没有指出您遇到的任何问题或任何其他形式的问题。这往往会导致帖子被关闭。
  • 这是 scanf 将换行符留在输入缓冲区中的常见问题,可能有很多重复。

标签: c


【解决方案1】:

使用scanf 而不是gets 将解决您的问题:

scanf("%s", n); // Read in your name

请注意,在读取任何这样的字符串时,您应该使用传递字符串长度的安全函数(例如来自 MSDN 的 scanf_s)。

【讨论】:

  • scanf 只会读取和存储名称的第一部分,但我想读取全名,包括空格
  • 哎呀,你花时间回复了!!
【解决方案2】:

代码不会跳过扫描“n”的值。 我相信当你运行程序时,你输入 Roll No,然后按键盘上的 ENTER 键。 这就是原因。 只要您按下 ENTER 键,转义序列 '\n' 就会保存在数组 n 中。您的 gets() 命令正在完美执行。


在第二种情况下,变量 'n' 存储转义序列,下一个变量 'f' 采用您接下来输入的字符串。


要使您的代码正常工作,只需像这样输入您的 scanf 语句:-

scanf("%d ",&r);

注意 %d 之后的空格

试试这个代码-

#include<stdio.h>

int main(void)
{
    int r;
    char n[30], f[30];
    printf("Enter your roll no");
    scanf("%d ",&r);  // I have inserted a space after %d
    printf("Enter your full name");
    gets(n);
    printf("Enter your full name of your father ");
    gets(f);
    printf("\nRoll no is %d ",r);
    printf("\nName is %s ",n);
    printf("\nFather name is %s ",f);
    return 0;
}

提示:- 你必须尽量不要使用gets() 和 puts()

你可以read more about it here

【讨论】:

  • 嗨 azad,由于 %d 后有空格,编译器将 '\n' 命令扫描为 ENTER 键,但它不会将其存储在任何变量中...因此您的变量 'n ' 可以使用。编码建议:- 请使用助记名称,它对调试和理解代码也很有帮助。
【解决方案3】:

解决问题的简单方法是在scanf();gets();之间添加fflush(stdin);

#include<stdio.h>
#include<conio.h>
void main()
{
    int r;
    char n[30],fn[30];
    clrscr();
    printf("\nEnter roll ");
    scanf("%d",&r);
    fflush(stdin);
    printf("\nEnter name ");
    gets(n);
    printf("\nEnter father name ");
    gets(fn);

    printf("\n\nRoll %d",r);
    printf("\nname %s",n);
    printf("\nfather name %s",fn);
    getch();
}
【解决方案4】:

我不知道为什么它会被跳过,但你可以做些什么来避免任何其他混淆,如 fflush(stdin) 或 fgets 等。

只需在下一行使用gets(string)。因此,当它跳过第一个获取命令时,它会转到另一个。

试试看 干杯, ;)

【讨论】:

    【解决方案5】:

    两个小时前我也遇到了同样的问题,但是要轻松解决这种情况,您只需在“scanf()”之后和“gets()”之前添加一个“getchar()"”,所以额外的“\n”转到“getchar()”,您可以在下一个“gets()”中随意输入。

    【讨论】:

      【解决方案6】:

      我也遇到了与上面提到的相同的问题.. 所以在此处提到的答案的帮助下并使用命中和试用方法,我发现当我们在使用scanf() 输入任何变量后按 Enter 键时,@ 987654322@ 存储在下一个gets() 函数中.. 下次它不会从键盘输入任何输入.. 为了避免这种情况,只需在scanf()gets() 之间使用getchar() 并且还记得getchar() 只需要 1 个字符.. 所以不要给 scanf() 任何额外的输入,因为这将再次被存储并将在 gets() 中使用,问题将保持不变...... 希望这会有所帮助.. 谢谢你。。

      【讨论】:

        猜你喜欢
        • 2021-11-07
        • 2017-11-10
        • 2021-10-03
        • 2019-08-01
        • 2021-03-19
        • 2019-07-06
        • 2012-06-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多