尽管声明,“未声明:首次使用函数错误”在while循环中

时间:2014-10-21 05:55:52

标签: c gcc

以下代码在unix机器上使用gcc编译时抛出以下错误:

eu.c: In function 'main':
eu.c:23: error: 'valuetoAdd' undeclared (first use in this function)
eu.c:23: error: (Each undeclared identifier is reported only once
eu.c:23: error: for each function it appears in.)

这是代码:我已经声明了变量valueToAdd。为什么不起作用?

#include <stdio.h>

int main (void)
{
    float input;
    float e = 0;
    int count = 0;
    int flag = 1;
    float valueToAdd = 0.0;

    do
    {
        printf("Enter input: ");
        scanf("%f", &input);
    } while (input < 0);

    while (flag)
    {
        valueToAdd = 1 / factorial(count);
        e += valuetoAdd;
        count++;

        flag = input < valueToAdd;
    }

    printf("The computed value of e is: %.15f", e);
    printf("%d terms were required", count++);

}

int factorial (int c)
{
    if (c == 0) return 1;
    else return c * factorial(c-1);
}

3 个答案:

答案 0 :(得分:2)

实际上,您没有声明valuetoAdd。您声明了valueToAdd,但这是一个不同的标识符。标识符在C中区分大小写。

答案 1 :(得分:2)

你应该valueToAdd使用valuetoAdd ..检查案例!!

答案 2 :(得分:0)

valueToAdd = 1 / factorial(count);
e += valuetoAdd;  <= Problem is here, valuetoAdd undeclared !