繁体   English   中英

在获取之前在 C. Scanf 中输入。 问题

[英]Input in C. Scanf before gets. Problem

我对 C 很陌生,我在向程序输入数据时遇到了问题。

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
   int a;
   char b[20];

   printf("Input your ID: ");
   scanf("%d", &a);

   printf("Input your name: ");
   gets(b);   

   printf("---------");

   printf("Name: %s", b);   

   system("pause");
   return 0;
}

它允许输入 ID,但它只是跳过输入的其余部分。 如果我像这样更改顺序:

printf("Input your name: ");
   gets(b);   

   printf("Input your ID: ");
   scanf("%d", &a);

它会起作用。 虽然,我不能改变订单,我需要它原样。 有人能帮我吗 ? 也许我需要使用其他一些功能。 谢谢!

尝试:

scanf("%d\n", &a);

gets 只读取 scanf 留下的 '\\n'。此外,您应该使用 fgets 而不是 gets: http : //www.cplusplus.com/reference/clibrary/cstdio/fgets/以避免可能的缓冲区溢出。

编辑:

如果上述方法不起作用,请尝试:

...
scanf("%d", &a);
getc(stdin);
...

scanf不消耗换行符,因此是fgets天敌。 没有好的黑客,不要把它们放在一起。 这两个选项都有效:

// Option 1 - eat the newline
scanf("%d", &a);
getchar(); // reads the newline character

// Option 2 - use fgets, then scan what was read
char tmp[50];
fgets(tmp, 50, stdin);
sscanf(tmp, "%d", &a);
// note that you might have read too many characters at this point and
// must interprete them, too

scanf 不会消耗 \\n,所以它会被 scanf 后面的 get 占用。 像这样在 scanf 之后刷新输入流。

#include <stdlib.h>
#include <string.h>

int main(void) {
   int a;
   char b[20];

   printf("Input your ID: ");
   scanf("%d", &a);
   fflush(stdin);
   printf("Input your name: ");
   gets(b);   

   printf("---------");

   printf("Name: %s", b);   

   system("pause");
   return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
        int a;
        char b[20];
        printf("Input your ID: ");
        scanf("%d", &a);
        getchar();
        printf("Input your name: ");
        gets(b);
        printf("---------");
        printf("Name: %s", b);
        return 0;
}



Note: 
  If you use the scanf first and the fgets second, it will give problem only. It will not read the second character for the gets function. 

  If you press enter, after give the input for scanf, that enter character will be consider as a input f or fgets.

你应该这样做。

    fgetc(stdin);
    scanf("%c",&c);
    if(c!='y')
    {
        break;
    }
    fgetc(stdin);

在通读gets后从scanf读取输入。

只需使用 2 个 get() 函数

当您想在 scanf() 之后使用 get() 时,请确保使用 2 个 gets() 函数,并针对上述情况编写如下代码:

int main(void) {
   int a;
   char b[20];

   printf("Input your ID: ");
   scanf("%d", &a);

//the change is here*********************
   printf("Input your name: ");
   gets(b);
   gets(b);   
//the change is here*********************

   printf("---------");

   printf("Name: %s", b);   

   system("pause");
   return 0;
}

解释(isaaconline96@gmail.com);

scanf("%d", &a); 无法读取返回值,因为%d只接受十进制整数。 因此,您在下一个scanf的开头添加一个\\n以忽略缓冲区内的最后一个\\n

然后, scanf("\\n%s", b); 现在可以毫无问题地读取字符串,但是scanf在找到空格时停止读取。 因此,将%s更改为%[^\\n] 它的意思是:“阅读所有内容,但\\n

scanf("\\n%[^\\n]", b);

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    int a;
    char b[20];

    printf("Input your ID: ");
    scanf("%d", &a);

    printf("Input your name: ");
    scanf("\n%[^\n]", b);
    //first \n says to ignore last 'return'
    //%[^\n] read until find a 'return'  
    printf("---------\n");
    printf("Name: %s\n\n", b);   

    system("pause");
    return 0;
}

scanf函数会在尝试解析字符以外的内容之前自动删除空格。 %c%n%[]是不删除前导空格的例外。

gets正在读取前一个scanf留下的换行符。 使用getchar();捕获换行符getchar();

scanf("%d", &a);
getchar(); // catches the newline character omitted by scanf("%d")
gets(b);

https://wpollock.com/CPlus/PrintfRef.htm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM