简体   繁体   English

如果输入字符代替 integer 如何终止程序

[英]How to terminate a program if a character is entered in place of an integer

I would like to validate the user input.我想验证用户输入。 For example, if I enter a character in the place of a integer, then the program should terminate.例如,如果我在 integer 的位置输入一个字符,那么程序应该终止。

The simplest way to read an integer from the user with basic input validation would be the following:通过基本输入验证从用户读取整数的最简单方法如下:

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

int main( void )
{
    int num;

    printf( "Enter a number: " );

    switch ( scanf( "%d", &num ) )
    {
    case 1:
        //input is valid
        break;
    case 0:
        printf( "That was not a valid number!\n" );
        exit( EXIT_FAILURE );
    default:
        printf( "Unexpected input error occurred!\n" );        
        exit( EXIT_FAILURE );
    }

    printf( "You entered the following valid input: %d\n", num );
}

However, this solution is not perfect, as it will accept for example 6sdfh4q as valid input for the number 6 , although that input should probably be rejected.但是,此解决方案并不完美,因为它将接受例如6sdfh4q作为数字6的有效输入,尽管该输入可能应该被拒绝。 This is because scanf is not designed for line-based user input.这是因为scanf不是为基于行的用户输入而设计的。

For line-based user input, it is probably better to use fgets instead of scanf .对于基于行的用户输入,使用fgets而不是scanf可能会更好。 In contrast to scanf , fgets will generally read one line at a time, so it is the perfect function for reading user input.scanf相比, fgets通常一次读取一行,因此它是读取用户输入的完美功能。 After reading one line of user input, you can attempt to convert the input to a number using the function strtol or sscanf .读取一行用户输入后,您可以尝试使用函数strtolsscanf将输入转换为数字。

Performing full input validation is rather complex in C. This is how I would write it:在 C 中执行完整的输入验证相当复杂。这就是我的编写方式:

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

int main( void )
{
    char buffer[1024], *p;
    long num;

    //prompt user for input
    printf( "Enter a number: " );

    //get one line of input from input stream
    if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
    {
        fprintf( stderr, "Unrecoverable error reading from input!\n" );
        exit( EXIT_FAILURE );
    }

    //make sure that entire line was read in (i.e. that
    //the buffer was not too small)
    if ( strchr( buffer, '\n' ) == NULL )
    {
        printf( "Line input was too long!\n" );
        exit( EXIT_FAILURE );
    }

    //attempt to convert string to number
    errno = 0;
    num = strtol( buffer, &p, 10 );
    if ( p == buffer )
    {
        printf( "Error converting string to number\n" );
        exit( EXIT_FAILURE );
    }

    //make sure that no range error occurred
    if ( errno == ERANGE )
    {
        printf( "Range error!\n" );
        exit( EXIT_FAILURE );
    }

    //make sure that remainder of line contains only whitespace,
    //so that input such as "6sdfh4q" gets rejected
    for ( ; *p != '\0'; p++ )
    {
        if ( !isspace( (unsigned char)*p ) )
        {
            printf( "Unexpected input encountered!\n" );
            exit( EXIT_FAILURE );
        }
    }

    //number was successfully converted, so we print it
    printf( "You entered the following valid number: %ld\n", num );
}

Example output of this program when entering a valid number:输入有效数字时此程序的示例输出:

Enter a number: 65
You entered the following valid number: 65

Example output of this program when entering invalid input:输入无效输入时此程序的示例输出:

Enter a number: 6sdfh4q
Unexpected input encountered!

When any character was being input in my program in the place of integers, it was the same as if I were inputting the integer 4256577. So, I simply wrote an if statement if(myint==4256577){exit(3);}.当在我的程序中输入任何字符代替整数时,就像我输入整数 4256577 一样。所以,我只写了一个 if 语句 if(myint==4256577){exit(3);} . Though I am uncertain about whether this number is defined somewhere or it just happened to show in my program that if I ran on another computer, it would be different, thus my program wouldn't eventually work虽然我不确定这个数字是在某个地方定义的,还是恰好在我的程序中显示,如果我在另一台计算机上运行,​​它会有所不同,因此我的程序最终无法工作

终止程序的正常方法是调用“退出”函数。

exit(ERROR_CODE);

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

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