【问题标题】:Validation of integer with character input in C program在 C 程序中使用字符输入验证整数
【发布时间】:2019-12-19 17:22:17
【问题描述】:

我编写这个程序来验证用户输入的选择(整数类型变量)。但问题是在有效输入之后,下一个无效输入(例如:字符类型变量)将不会存储在整数变量(选择)中。我该如何解决这个问题?

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#pragma warning (disable:4996)

void main()
{
    int selection;
    while (1)
    {
        while (1)
        {
            printf("Enter Your Selection (0-4) > ");
            scanf("%d", &selection);
            rewind(stdin);
            if (!selectionCheck(&selection, 0, 4))
                printf("Invalid\n");
            else break;
        }
        printf("Success\n");
    }

    system("pause");
}

int selectionCheck(int *input, int min, int max)
{
    char str[100] = "";
    itoa(*input, str, 10);
    if (isdigit(str[0]))
    {
        if (*input < min || *input > max)
            return 0;
        else return 1;
    }
    else
    {
        return 0;
    }
}

【问题讨论】:

  • 不是一个答案,但是如果你从来没有给它赋值,你为什么要传递一个指向selectionCheck() 的指针?,只需传递一个int

标签: c validation integer character


【解决方案1】:

一些注意事项: 1)你没有检查scanf()的返回值,这个很有用:负返回表示输入的字符不能转换为int(因为“%d”格式),返回value 等于 0 表示输入为空(未输入任何字符)。

2) 如果用户输入了错误的字符(不是数字),输入缓冲区将保持忙碌,直到您以其他方式读取它。好主意是在此处使用额外的scanf("%s") 将任何字符读取为字符串,因此在此调用后缓冲区将为空。在这里使用rewind() 是不够的。

3) 对于isdigit(),无需额外检查selectionChecking() 中的输入,因为scanf() 中的“%d”格式不允许读取除数字之外的任何其他内容。

4) 无需在 selectionChecking() 调用中传递指向 selection 值的指针 - 将其作为值传递就足够了。

那么,试试下面这个:

// declaration of 'selectionCheck()'
int selectionCheck(int input, int min, int max);

void main()
{
    int selection;
    while (1)
    {
        while (1)
        {
            printf("Enter Your Selection (0-4) > ");

            int ret = scanf("%d", &selection);
            if (ret < 0) // invalid characters on input
            {
                printf("Invalid characters\n");
                scanf("%s"); // empty buffer, reading it as string and putting readed characters to nowhere ;)
                continue; // go to top of loop
            }

            if (ret == 0) // empty input
            {
                printf("No (empty) input\n");
                continue; // go to top of loop
            }

            // here 'ret' is greather than 0, so valid number was entered

            if (selectionCheck(selection, 0, 4)) // is value between 0 and 4 ?
                break; // yes, success, break current loop!

            printf("Invalid value\n");
        }

        printf("Success\n");
    }

    system("pause");
}

int selectionCheck(int input, int min, int max)
{
    if (input < min || input > max)
        return 0;
    else 
        return 1;
}

当然,你可以把'selectionCheck()'写得更简洁:

int selectionCheck(int input, int min, int max)
{
    return (input < min || input > max) ? 0 : 1;
}

或者简单地说:

int selectionCheck(int input, int min, int max)
{
    return (input >= min && input <= max);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-02
    • 2020-04-30
    • 2022-09-28
    • 2019-06-02
    • 2021-12-14
    • 2013-02-20
    • 2013-10-09
    • 1970-01-01
    相关资源
    最近更新 更多