11

我对 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);

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

4

8 回答 8

12

尝试:

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

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

编辑:

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

...
scanf("%d", &a);
getc(stdin);
...
于 2010-03-02T20:34:01.013 回答
8

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
于 2010-03-02T20:49:09.547 回答
3

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;
}
于 2010-11-16T11:50:07.157 回答
1
#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.
于 2010-03-03T04:10:10.447 回答
0

你应该这样做。

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

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

于 2013-08-04T09:50:07.693 回答
0

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

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

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

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

于 2017-01-05T04:55:28.203 回答
0

只需使用 2 个 gets() 函数

当您想在 scanf() 之后使用 gets() 时,请确保使用 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;
}
于 2016-01-09T09:51:05.637 回答
0

scanf("%d", &a);无法读取返回值,因为%d只接受十进制整数。\n因此,您在下一个开头添加 ascanf以忽略\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;
}
于 2016-07-05T17:30:34.117 回答