繁体   English   中英

如何在 C 中读取 Enter 键作为输入?

[英]How do I read in the Enter key as an input in C?

如何在 C 中读取Enter键作为输入? 我想要一些这样的程序:

"Please enter a key to go to next line"

如何在 C 中添加此类选项?

我是编程新手。

您可以使用stdio.h中的getc function 等待某个键被按下。 此 C 代码将等待按下任何键,然后 output “续:”:

#include <stdio.h>

int main(){
    printf("Press any key to continue");
    getc(stdin);
    printf("Continued!");
    return 0;
}

如果我理解你的问题,你可以写:

printf("Press ENTER key to Continue\n");  
getchar();

或者

printf("Press Any Key to Continue\n");
getch();

要打印出输入的密钥,请执行以下操作:

read_in_any_key.c:

#include <stdio.h>

int main()
{
    printf("Press any key followed by Enter, or just Enter alone, to continue: ");
    int c = getc(stdin);
    printf("The first character you entered is decimal %i (\"%c\").\n", c, c);

    return 0;
}

示例运行和 output。 请注意,在最后一个示例中,我单独按Enter 键,因此将其打印为换行符。 每个键的十进制数字可以在 ASCII 表中找到,如下所示: https://en.wikipedia.org/wiki/ASCII#Control_code_chart

 eRCaGuy_hello_world/c$ gcc -Wall -Wextra -Werror -O3 -std=c17 read_in_enter_key.c -o bin/a && bin/a Press any key followed by Enter, or just Enter alone, to continue: abc The first character you entered is decimal 97 ("a").
 eRCaGuy_hello_world/c$ gcc -Wall -Wextra -Werror -O3 -std=c17 read_in_enter_key.c -o bin/a && bin/a Press any key followed by Enter, or just Enter alone, to continue: def The first character you entered is decimal 100 ("d").
 eRCaGuy_hello_world/c$ gcc -Wall -Wextra -Werror -O3 -std=c17 read_in_enter_key.c -o bin/a && bin/a Press any key followed by Enter, or just Enter alone, to continue: FTL The first character you entered is decimal 70 ("F").
 eRCaGuy_hello_world/c$ gcc -Wall -Wextra -Werror -O3 -std=c17 read_in_enter_key.c -o bin/a && bin/a Press any key followed by Enter, or just Enter alone, to continue: The first character you entered is decimal 10 (" ").

要仅在单独按下Enter时继续,之前没有其他内容,请执行以下操作:

read_until_only_enter_key.c:

#include <stdbool.h> // For `true` (`1`) and `false` (`0`) macros in C
#include <stdio.h>   // For `printf()`

// Clear the stdin input stream by reading and discarding all incoming chars up to and including
// the Enter key's newline ('\n') char. Once we hit the newline char, stop calling `getc()`, as
// calls to `getc()` beyond that will block again, waiting for more user input.
void clearStdin()
{
    // keep reading 1 more char as long as the end of the stream, indicated by the newline char,
    // has NOT been reached
    while (true)
    {
        int c = getc(stdin);
        if (c == EOF || c == '\n')
        {
            break;
        }
    }
}

// Returns true if only Enter was pressed, and false otherwise
bool getOnlyEnterKeypress()
{
    bool onlyEnterPressed = false;
    printf("Press ONLY the Enter key to continue: ");
    int firstChar = getc(stdin);
    if (firstChar == '\n')
    {
        printf("Good! You pressed ONLY the Enter key!\n");
        onlyEnterPressed = true;
    }
    else
    {
        printf("You failed. You pressed another key first.\n");
        clearStdin();
    }
    return onlyEnterPressed;
}

// int main(int argc, char *argv[])  // alternative prototype
int main()
{
    while (!getOnlyEnterKeypress()) {};

    return 0;
}

示例运行和 output。 请注意,当我只按下 Enter时,程序直到最后才退出,之前没有更多的键。

eRCaGuy_hello_world/c$ gcc -Wall -Wextra -Werror -O3 -std=c17 read_until_only_enter_key.c -o bin/a && bin/a
Press ONLY the Enter key to continue: a
You failed. You pressed another key first.
Press ONLY the Enter key to continue: abc
You failed. You pressed another key first.
Press ONLY the Enter key to continue: w
You failed. You pressed another key first.
Press ONLY the Enter key to continue:
Good! You pressed ONLY the Enter key!

只有另一个答案正确地触及了这一点。 不要假设您的用户会服从您,即使他或她打算这样做。

// Ask user to do something
printf( "Press Enter to continue: " );
fflush( stdout );

// Wait for user to do it
int c;
do c = getchar();
while ((c != EOF) and (c != '\n'));

这将丢弃其他输入,直到按下 Enter 键或到达 EOF,将您的输入 stream 留在已知的 state 中。

如果您希望获得无缓冲的输入,那是完全不同的蠕虫罐头。

conio.h header C 下提供了一个 function getch等待字符 Z718E6221F39891D。

printf("Enter any key to continue");
getch();// will wait until a character is entered
//do sth else 

如果要检查输入了哪个字符,

char ch = getch();
if(ch == 'a')
  printf("You entered a");

暂无
暂无

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

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