C - 按 Enter 键继续?

问题描述 投票:0回答:1
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();
}

您好,我被困在“暂停”功能上。按 ENTER 继续不起作用?

c
1个回答
3
投票

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

#include <stdio.h>
void clrKyb(void) {
    while ((c = getchar()) != '\n' && c != EOF) {};
}
void pause(void) {
    printf("Press <ENTER> to continue.");
    clrKyb();
}
© www.soinside.com 2019 - 2024. All rights reserved.