如何在C中修复scanf()的返回值?

时间:2015-09-24 17:11:43

标签: c

我正在为从if ( A COMPARISON STATEMENT ) { $(".SalePrice", this).text("From " + salePrice) } else { $(".p-price", this).text("From " + price); } 获取输入的编程类编写一些C代码。该程序用于检查输入的格式,如果输入格式不正确,则终止。我的问题是该程序没有正确读取格式化的输入。

它需要执行的第一次检查是stdin。如果我错了,请纠正我,但int应该跳过空格,直到找到scanf()为止时int作为参数。然后它应该返回%d:它收到的成功输入的数量。我将此int值分配给变量,以便我可以检查错误。我的return语句只有一个参数,我确信它正在接收正确的输入,但由于某种原因,返回值不正确。

我的代码如下:

scanf()

程序不应该提示用户输入,我们只需要将输入重定向到来自文件。我的测试文件包含以下内容:

double stddev(double sum, double sum_of_squares, int n){ double variance, result; variance = ((n * sum_of_squares) - pow(sum, 2)) / (n * (n - 1)); result = sqrt(variance); return result; } int nextinput() { int true = 1; int false = 0; int current = getchar(); if (isspace(current)) { while (isspace(current = getchar())) { if (current == '\n') { return false; } } } else { return false; } ungetc(current, stdin); return true; } int main() { char current_char; int ret, current_int, control; int max_char = 0; int int_sum = 0; int max_int = INT_MIN; int min_int = INT_MAX; int count = 0; double current_double, double_avg, standard_dev; double double_sum = 0; double double_squared_sum = 0; while ((control = getchar()) != EOF) { ungetc(control, stdin); ret = scanf("%d", &current_int); printf("%d", ret); if (ret == 1) { ret = nextinput(); if (ret == 1) { ret = scanf("%c", &current_char); if (ret == 1) { ret = nextinput(); if (ret == 1) { ret = scanf("%lf", &current_double); if (ret == 1) { int_sum += current_int; double_sum += current_double; double_squared_sum += pow(current_double, 2); count++; if (current_int > max_int) { max_int = current_int; } if (current_int < min_int) { min_int = current_int; } if (current_char > max_char) { max_char = current_char; } } else { fprintf(stderr, "Invalid input. A double was " "missing\n"); return EXIT_FAILURE; } } else { fprintf(stderr, "Invalid input. There was a space " "missing or a new line found after a " "character."); return EXIT_FAILURE; } } else { fprintf(stderr, "Invalid input. A character was missing." "\n"); return EXIT_FAILURE; } } else { fprintf(stderr, "Invalid input. There was a space missing or " "a new line found after an integer.\n"); } } else { fprintf(stderr, "Invalid input. An integer was missing.\n"); return EXIT_FAILURE; } } standard_dev = stddev(double_sum, double_squared_sum, count); double_avg = double_sum / count; printf("Sum of integers: %d\n", int_sum); printf("Max integer seen: %d\n", max_int); printf("Min integer seen: %d\n", min_int); printf("Max char value seen: %d\n", max_char); printf("Average of doubles: %.3lf\n", double_avg); printf("Standard deviation of doubles: %.3lf\n", standard_dev); return EXIT_SUCCESS; }

1

start file

我使用printf()检查了我的变量,并且end file被设置为1,但是我的ret语句中的所有代码都没有被执行。然后,if设置为-1,代码给了我错误消息。它看起来像这样:

ret

start output Invalid input. An integer was missing. 1-1

有人可以帮我理解为什么会这样吗?我的程序还有更多内容,但在我的程序终止之前似乎没有执行。

1 个答案:

答案 0 :(得分:0)

关于这个功能:

int nextinput()
{
    int true = 1;
    int false = 0;
    int current = getchar();

    if (isspace(current))
    {
        while (isspace(current = getchar()))
        {
            if (current == '\n')
            {
                return false;
            }
            else
            {}
        }
    }
    else
    {
        return false;
    }

    ungetc(current, stdin);
    return true;
}

nextinput()函数的更好定义是:

#include <stdbool.h>

int nextInput() // for readability, use 'camel case' for function and variable names
{

    int current;

    while (isspace(current = getchar()))
    {
        if (current == '\n')
        {
            return false;
        }
    }

    ungetc(current, stdin);
    return true;
}

但是,main()函数包含几个问题,我甚至没有查看stddev()函数。

强烈建议通过代码查看您的(错误/意外内容)输入文件实际发生的情况。

建议阅读getchar()和scanf()以及ungetc()的手册页,以便更好地了解它们的作用以及它们返回的值。

建议总是在比较中将字面值放在左侧,因此编译器会捕获在需要=时输入==这样的错误