【问题标题】:How can I make the number entered by the user to be a natural number only?How can I make the number entered by the user to be a natural number only?
【发布时间】:2022-12-01 23:36:33
【问题描述】:

I want to make sure that the character the user enters only works for a natural number. I can do it for letters and symbols because it's a simple "else". But if the user enters a number that contains a decimal point, I want the program to stop with an error code. I know that it simply cuts off the decimal point if I read double into %d.

【问题讨论】:

  • You should post your code. It's kind of hard to infer what you are trying to do. Input + %d seems like yet another scanf question. Do yourself a favor and don't use scanf. Read the bytes and loop through them looking for a decimal point. Or just use strtod, strtol, etc...
  • Your question is identical to this question, except that you will have to add an additional check whether the number is positive or not (a natural number is an integer that is positive or at least not negative, depending on how you define it).
  • Thank you, guys!

标签: c numbers scanf


【解决方案1】:

I generally do not recommend that you use scanf for user input, as that is not what the function was designed to do.

Your question is nearly identical to this question. The only difference is that the linked question asks about how to validate the user input as an int, whereas you are asking about how to validate the user input as a natural int, i.e. a positive or non-negative integer (depending on how you define natural number). Therefore, all you must do is use one of the answers from that question and add an additional check whether the number is positive or non-negative.

In the code below, I have modified the function get_int_from_user from my answer to the question mentioned above, by adding this additional check, and have changed the name of the function to get_natural_int_from_user.

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

int get_natural_int_from_user( const char *prompt )
{
    //loop forever until user enters a valid number
    for (;;)
    {
        char buffer[1024], *p;
        long l;

        //prompt user for input
        fputs( prompt, stdout );

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

        //make sure that entire line was read in (i.e. that
        //the buffer was not too small)
        if ( strchr( buffer, '
' ) == NULL && !feof( stdin ) )
        {
            int c;

            printf( "Line input was too long!
" );

            //discard remainder of line
            do
            {
                c = getchar();

                if ( c == EOF )
                {
                    fprintf( stderr, "Unrecoverable error reading from input!
" );
                    exit( EXIT_FAILURE );
                }

            } while ( c != '
' );

            continue;
        }

        //attempt to convert string to number
        errno = 0;
        l = strtol( buffer, &p, 10 );
        if ( p == buffer )
        {
            printf( "Error converting string to number!
" );
            continue;
        }

        //make sure that number is representable as an "int"
        if ( errno == ERANGE || l < INT_MIN || l > INT_MAX )
        {
            printf( "Number out of range error!
" );
            continue;
        }

        //make sure that number is non-negative
        if ( l < 0 )
        {
            printf( "Natural numbers must be non-negative!
" );
            continue;
        }

        //make sure that remainder of line contains only whitespace,
        //so that input such as "6sdfj23jlj" gets rejected
        for ( ; *p != '
猜你喜欢
  • 2022-12-02
  • 2022-12-02
  • 2022-12-02
  • 2022-12-27
  • 2022-12-01
  • 2022-12-01
  • 2022-12-02
  • 2022-12-01
  • 2022-12-02
相关资源
最近更新 更多