c - 如何限制我的用户输入

标签 c

我一直在创建一个程序,要求用户输入一个字符值,但如果他们输入多个字符,它将移至下一个函数并中断程序。我添加了第二种方法,该方法在输入多个输入时运行。

最佳答案

您的问题不是限制写入cInput的字符数;而是限制写入cInput的字符数。格式说明符 %1s 已经做到了这一点。您的问题是在输入缓冲区中留下未处理的字符之一。如果后续输入无法处理这些字符,则需要从缓冲区中删除所有字符。例如,如果您在 then 缓冲区中留下一个字母字符,但稍后使用 %d 读取,该函数将立即返回(因为隐式还缓冲了一个换行符),但该字符将保持缓冲状态,因为它是不是小数。如果您从未清除缓冲区,这将无限期地继续。

对于单个字符,可以检查该字符是不是换行符,然后重复获取字符,直到找到换行符,如下:

scanf("%c", &cInput ) ;
while( cInout != '\n' && getchar() != '\n' ) { } // flush buffered line

如果你想确保用户只输入一个字符,那么你可以修改上面的内容:

scanf("%c", &cInput ) ;   // Get a single character

if( cInput != '\n' &&     // If the input is not "empty",
    getchar() != '\n' )   // but the first character entered was not
                          // immediately followed by a newline...
{
    // Flush to the end of the entered line
    while( getchar() != '\n' ) { }

    // Invalidate the input to force retry
    cInput = 0 ;
}

至少一个字符将被缓冲 - 至少一个换行符。有效的答案将包含两个字符,一个位于 cInput 中,一个换行符。上面的 if(...) 条件读取第二个字符(如果有)(使用 cInput 的短路评估),并检查它是否是输入(换行)。如果不是,它会读取所有缓冲的字符,然后使 cInput 无效(例如,如果输入了 "No-way\n",则 cinput 包含 'N'

对于数字输入,您只需读取字符直到找到换行符:

scanf("%d", &nValue);
while( getchar() != '\n' ) { } // flush buffered line 

如果尾随非数字字符会使整个输入无效,您需要检查后面的字符是否为换行符。

int converted = scanf("%d", &nValue);
if( converted == 0 || getchar() != '\n' )
{
    valid_input = false ;
    while( getchar() != '\n' ) { } // flush buffered line 
}

请注意,还有其他可能的解决方案。这是我的首选解决方案。

当应用于您的函数时(经过其他简化):

int intAskUser(void)
{
    char cInput = 0 ;

    while( cInput != 'N' && cInput != 'Y' )
    {
        printf("Do you want to enter a value? Y or N\n");
        scanf("%c", &cInput ) ;
        if( cInput != '\n' && getchar() != '\n' )
        {
            while( getchar() != '\n' ) { } // flush buffered line
            cInput = 0 ;
        }
        else
        {
            cInput = toupper(cInput) ;
        }
    }

    // Return answer code 0 to 1
    return (cInput == 'N') ? 0 : 1 ;
}

int getValue(int nLower, int nUpper)
{
    assert( nLower < nUpper ) ;  // precondition check

    int nValue = 0 ; 
    bool valid_input = false ;

    while( !valid_input )
    {
        printf("Enter a value between %d and %d: ", nLower, nUpper ) ;
        int converted = scanf("%d", &nValue);
        if( converted == 0 || getchar() != '\n' )
        {
            valid_input = false ;
            while( getchar() != '\n' ) { } // flush buffered line 
        }

        valid_input = nValue >= nLower && 
                      nValue <= nUpper ;

        if( !valid_input )
        {
            printf( "Please try again. " );
        }
    } 

    printf("Value: %d", nValue);
    return nValue;
}

请注意,toupper() 需要包含 ctype.h,并且类型 bool 需要 stdbool.h.

关于c - 如何限制我的用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42318747/

相关文章:

c - 没有默认大小写的 Objective-C 枚举 "exhaustive"开关的行为

c - 填充二维数组 C

c - DSP 的快速二维卷积

c++ - 在其他计算机上导入 RSA key 时出现问题

c - 套接字死锁

c struct 和 memcpy(字节数组)

c - 直接打印到文本视频内存时出现意外输出

c - Rake 构建 C 应用程序

c++ - 安卓NDK : Not picking up CPP includes

c - 使用 Zeller 的一致性确定星期几