Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在获取之前输入C.Scanf。问题_C_Input_Scanf_Gets - Fatal编程技术网

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

在获取之前输入C.Scanf。问题,c,input,scanf,gets,C,Input,Scanf,Gets,我是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);

我是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;
}
它会起作用的。虽然,我不能改变顺序,我需要它就像是。有人能帮我吗?也许我需要使用一些其他功能。谢谢

试试看:

scanf("%d\n", &a);
获取只读取scanf留在中的“\n”。此外,还应使用fgets not get:以避免可能的缓冲区溢出

编辑:

如果上述方法无效,请尝试:

...
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
#包括
#包括
#包括
内部主(空){
INTA;
charb[20];
printf(“输入您的ID:”);
scanf(“%d”和“&a”);
getchar();
printf(“输入您的姓名:”);
获取(b);
printf(“-----------”);
printf(“名称:%s”,b);
返回0;
}
注:
如果您先使用scanf,然后使用fgets,则只会出现问题。它不会读取gets函数的第二个字符。
如果按Enter,在输入SCANF后,输入字符将被视为输入F或FGET。

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;
}
#包括
#包括
内部主(空){
INTA;
charb[20];
printf(“输入您的ID:”);
scanf(“%d”和“&a”);
fflush(stdin);
printf(“输入您的姓名:”);
获取(b);
printf(“-----------”);
printf(“名称:%s”,b);
系统(“暂停”);
返回0;
}

你应该这样做

    fgetc(stdin);
    scanf("%c",&c);
    if(c!='y')
    {
        break;
    }
    fgetc(stdin);
要在读取gets后从scanf读取输入,只需使用2个gets()函数即可

如果要在scanf()之后使用get(),请确保使用两个get()函数,对于上述情况,请编写如下代码:

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)

#包括
#包括
#包括
内部主(空){
INTA;
charb[20];
printf(“输入您的ID:”);
scanf(“%d”和“&a”);
printf(“输入您的姓名:”);
scanf(“\n%[^\n]”,b);
//first\n表示忽略最后一个“return”
//%[^\n]读取直到找到“返回”
printf(“-----------\n”);
printf(“名称:%s\n\n”,b);
系统(“暂停”);
返回0;
}

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

get
正在读取上一个
scanf
留下的换行符。使用
getchar()捕捉换行符


Hm.我试过scanf(“%d\n”和&a);但它似乎不起作用。在我输入ID之后,我只是看不到程序在做任何其他事情。哇!谢谢这工作完美无瑕!感谢您对fgets的建议。一定会用的!!谢谢大家!
scanf(“\n”)
不会按照您的想法执行操作-它会使用所有连续的空白字符,因此在缓冲非空格字符之前不会返回。gets(3):“gets()函数无法安全使用。由于它缺少边界检查,并且调用程序无法可靠地确定下一条传入线路的长度,因此使用此函数可使恶意用户通过缓冲区溢出攻击任意更改正在运行的程序的功能。这是ongly建议在所有情况下都使用fgets()函数(参见FSA)。“不要使用它。更简单地说:如果你使用
get
,飞行狂犬攻击OCELOT会撕掉你的眼袋。所以不要。
scanf
是邪恶的-
get(b)更改为
scanf(“%19[^\n]”,b)scanf(“%d%*c”和&a)
应该可以工作。
%*c
术语导致
scanf
读取一个字符(换行符),但星号导致丢弃该值。这将使
scanf
在不需要单独函数调用的情况下接受换行符。您可以将这两个选项视为具有同等地位,但
scanf
很难正确使用,因此最好避免完全使用它(请参阅comp.lang.c常见问题解答链接)。只需使用选项2。在一些C库中,fflush(stdin)和fflush(NULL)都将刷新stdout和stderr,但这是完全不可移植的!欢迎来到SO!请记住,英语并不是这里每个人的第一语言,所以尽量发布语法正确的答案,而不是用“u”来表示“你”——这在谷歌翻译中是行不通的!添加了解释@Coatless
    fgetc(stdin);
    scanf("%c",&c);
    if(c!='y')
    {
        break;
    }
    fgetc(stdin);
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;
}
#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("%d", &a);
getchar(); // catches the newline character omitted by scanf("%d")
gets(b);