Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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-按Enter键继续? void clrKyb(void) { charc[30]; scanf(“%s”,c); 对于(int i=0;i_C - Fatal编程技术网

C-按Enter键继续? void clrKyb(void) { charc[30]; scanf(“%s”,c); 对于(int i=0;i

C-按Enter键继续? void clrKyb(void) { charc[30]; scanf(“%s”,c); 对于(int i=0;i,c,C,您好,我一直在使用“暂停”功能。按ENTER键继续不起作用?只需使用getchar(),它将返回从stdin读取的第一个字符,并等待ENTER键 void clrKyb(void) { char c[30]; scanf("%s",c); for(int i = 0; i < 30; i++){ if(c[i] == '\n'){ i = 30; } } } void pauses(void) {

您好,我一直在使用“暂停”功能。按ENTER键继续不起作用?

只需使用getchar(),它将返回从stdin读取的第一个字符,并等待ENTER键

void clrKyb(void)
{
    char c[30];

    scanf("%s",c);

    for(int i = 0; i < 30; i++){
        if(c[i] == '\n'){
            i = 30;
        }
    }
}

void pauses(void)
{
    printf("Press <ENTER> to continue:");
    clrKyb();
}
#包括
无效clrKyb(无效){
而((c=getchar())!='\n'&&c!=EOF){};
}
作废暂停(作废){
printf(“按以继续”);
clrKyb();
}

我觉得这是家庭作业。下次,请事先通知我们,以便我们提供指导而不是答案。

%s
尝试阅读单词。如果只按Enter键,则没有单词,因此它将继续等待。
scanf
不安全。改用
scanf_s
@戴:这不是微软特有的吗?可能是重复的吗?还有@KeithThompson,如果是的话,它们就不再是了:它们在C11中是标准化的:我还需要它来读取超过1个字符的内容。例如“abcd”。@user3013760但您确实需要返回该输入吗?因为你的例子没有做到这一点。此函数能够检测1个以上的字符和enter,但只返回第一个字符。您不需要检查getchar()的返回值是否为“\n”,如果继续,您就知道按了enter键。它会在其他函数中返回,这些函数都可以正常工作。我只是停留在“暂停”上。按继续执行代码的其余部分。@user3013760这样您就不需要知道在输入之前输入的内容的实际内容了?在这种情况下,此解决方案将起作用。查看我对我的评论的编辑。如果用户键入
abcd
,然后键入Return,
getchar()
将只读取
a
。该行的重置,包括尾随的
'\n'
,将保留在stdin中,供下一个输入操作读取。
#include <stdio.h>
void clrKyb(void) {
    while ((c = getchar()) != '\n' && c != EOF) {};
}
void pause(void) {
    printf("Press <ENTER> to continue.");
    clrKyb();
}