windows 如何处理用户输入并在用户按ESC键时关闭程序

6l7fqoea  于 2022-11-26  发布在  Windows
关注(0)|答案(1)|浏览(197)

我想做一个程序,它会无休止地工作,直到用户按下ESC键。我需要处理用户的输入,因为我输入的值应该是整数。我该怎么做呢?

#include <stdio.h>

int main(){
    int num,factorial; 
    while(1){
        
        factorial = 1;
        printf("Enter Number : ");
        scanf("%d",&num);
        for(int i = 1 ; i<=num ; i++){ 
            factorial *= i;
        }
        printf("Factorial : %d\n",factorial); 
    }
    return 0;
}

程序应计算给定数字的阶乘。
我试过了,但它做了一个无休止的循环。

if(scanf("%d", &sayi) != 1){
    printf("Error Occured.\n");
    continue; 
}
zlhcx6iw

zlhcx6iw1#

在Microsoft Windows上,无法使用C标准程式库提供的函式来侦测ESC键。
但是,可以使用下列platform特定函数来执行此操作:

当使用这些函数时,你必须自己编写基本的输入功能。

  • 决定何时将所述输入回送给所述用户,以及
  • 当用户按下退格键时删除最后一个字符。

因此,如果你正在寻找一个简单的解决方案,这可能不是你想要的。继续使用C标准库提供的函数,并让用户做一些其他的事情来表明他们已经完成,例如告诉用户输入数字-1,这将是更容易的。
如果你想做一些更优雅的事情,比如让用户在完成时输入qquit而不是-1,那么你可以使用函数fgets而不是scanf,以便首先将用户的一行输入作为一个字符串读取,然后将这个字符串与qquit进行比较。并且只有当比较失败时,才尝试将字符串转换为数字,例如使用函数strtol
以下是一个示例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//forward declaration of function
void get_line_from_user( char *buffer, int buffer_size );

int main()
{
    while ( 1 )
    {
        char line[200], *p;
        long num, factorial = 1;

        //prompt user for input
        printf("Enter number, or \"q\" to quit: ");

        //read one line of input from user
        get_line_from_user( line, sizeof line );

        //determine whether user wants to quit
        if ( line[0] == 'q' )
        {
            printf( "Quitting program...\n" );
            exit( EXIT_SUCCESS );
        }

        //attempt to convert input string to a number
        num = strtol( line, &p, 10 );

        //verify that conversion was successful
        if ( p == line )
        {
            printf( "Input error!\n" );
            continue;
        }

        //perform calculations
        for( int i = 2; i <= num; i++ )
        { 
            factorial *= i;
        }

        //print the result
        printf( "Factorial : %ld\n", factorial ); 
    }

    return 0;
}

//This function will read exactly one line of input from the
//user. On failure, the function will never return, but will
//print an error message and call "exit" instead.
void get_line_from_user( char *buffer, int buffer_size )
{
    char *p;

    //attempt to read one line of input
    if ( fgets( buffer, buffer_size, stdin ) == NULL )
    {
        printf( "Error reading from input\n" );
        exit( EXIT_FAILURE );
    }

    //attempt to find newline character
    p = strchr( buffer, '\n' );

    //make sure that entire line was read in (i.e. that
    //the buffer was not too small to store the entire line)
    if ( p == NULL )
    {
        //a missing newline character is ok if the next
        //character is a newline character or if we have
        //reached end-of-file (for example if the input is
        //being piped from a file or if the user enters
        //end-of-file in the terminal itself)
        if ( getchar() != '\n' && !feof(stdin) )
        {
            printf( "Line input was too long!\n" );
            exit( EXIT_FAILURE );
        }
    }
    else
    {
        //remove newline character by overwriting it with
        //null character
        *p = '\0';
    }
}

此程序具有以下行为:

Enter number, or "q" to quit: 5
Factorial : 120
Enter number, or "q" to quit: 3
Factorial : 6
Enter number, or "q" to quit: Test
Input error!
Enter number, or "q" to quit: 7
Factorial : 5040
Enter number, or "q" to quit: 5
Factorial : 120
Enter number, or "q" to quit: 6
Factorial : 720
Enter number, or "q" to quit: q
Quitting program...

相关问题