输入无效输入时无限循环

时间:2015-09-26 13:02:42

标签: c

我是C编程的新手,我想检查除第一个元素之外的所有数组元素都是整数。

我编写了以下代码,但是一旦插入错误的输入,循环就永远不会停止。

bool validate_int(char input[]){
    fgets(input,10, stdin);
    for(int i = 1; i < strlen(input); ++i) {
        if(!isdigit(input[i])){
            i = 1;
            fgets(input,10, stdin);
        }
        else{
        }
    }
    return true;
}

4 个答案:

答案 0 :(得分:2)

您的代码存在一些小问题。这是一种更好的方法(未经测试)

bool validate_int(char input[]) /* Bad function name; See @Filipe's comment */
{
    for(;;) /* Infinite loop */
    {
        if(fgets(input, 10, stdin) == NULL) /* If fgets failed */
        {
            puts("fgets failed");
            return false; 
        }

        int i, len = strlen(input); 

        if(len > 0 && input[len - 1] == '\n') /* If there is a newline character at the end of input */
            input[--len] = '\0'; /* Replace the '\n' with '\0' and decrement len */

        if(!isalpha(input[0])) /* If the first character of input is not an alphabet */
            continue; /* Loop again */

        if(len == 1) /* There is no number */
            continue;

        for(i = 1; i < len; ++i)
        {
            if(!isdigit(input[i])) /* If not a digit */
                continue; /* Loop again */
        }

        break; /* Get out of the loop */
    }

    return true;
}

更好的方法是将输入和验证分成两个单独的函数(未经测试)

bool getInput(char input[])
{
    if(fgets(input, 10, stdin) == NULL) /* If fgets failed */
    {
        puts("fgets failed");
        return false; 
    }

    int len = strlen(input); 

    if(len > 0 && input[len - 1] == '\n') /* If there is a newline character at the end of input */
        input[--len] = '\0'; /* Replace the '\n' with '\0' and decrement len */

    return true;
}

bool validate(char input[])
{
    if(!isalpha(input[0])) /* If the first character of input is not an alphabet */
        return false;

    int i, len = strlen(input);

    if(len == 1) /* There is no number after the character */
        return false;

    for(i = 1; i < len; ++i)
    {
        if(!isdigit(input[i])) /* If not a digit */
            return false;
    }

    return true;
}

并且在调用函数(再次,未经测试)

char input[10];
if(getInput(input))
{
    if(validate(input))
    {
        puts("Input is in correct format");
    }
    else
    {
        puts("Input is in wrong format");
    }
}
else
{
    puts("Failed to get input");
}

答案 1 :(得分:2)

这是我要采取的另一种方法,这是必须更清洁的IMO:

这是您的验证功能:

bool customValidation(char *string) 
{
    int len = strlen(string);
    if (!isalpha(string[0]) || (len > 1 && !isdigit(string[1])))
        return false;

    for (int i = 1; i < len && string[i] != '\n'; ++i)
        if (!isdigit(string[i]))
            return false;

    return true;
}

这就是你如何使用它:

char input[10];
do
{
    fgets(input, 10, stdin);
} while (!customValidation(input));

显然你应该将customValidation()重命名为更重要的东西。

答案 2 :(得分:1)

试试这个

bool validate_int(char input[]){
    bool valid;

    do{
        valid = false;
        fgets(input,10, stdin);
        for(int i = 1; input[i] && input[i] != '\n'; ++i) {
            if(!isdigit(input[i])){
                valid = false;
                break;
            } else {
                valid = true;
            }
        }
    }while(!valid);
    return true;
}

答案 3 :(得分:-1)

检查一下:

int a_Length = 10;
char input[a_Length];
fgets(input,a_Length, stdin);
for(int i = 1; i < strlen(input); ++i) {
    if(!isdigit(input[i])&& input[i]!='\n'){
        i = 0;
        printf("Again try: ");
        if (input[a_Length - 1 ]!='\n')
            getchar();
        fgets(input,10, stdin);
    }
}