首页 文章

GCC参数类型错误?

提问于
浏览
0

我对此很新,所以我确定这是一个业余的错误 . 我正在尝试编写一个基本的金融计算器,并在我尝试编译时继续遇到此错误:

findn.c:在函数'main'中:findn.c:36:3:警告:格式'%f'需要类型为'float *'的参数,但参数2的类型为'double'[-Wformat] findn.c: 50:3:警告:格式'%f'需要类型为'float *'的参数,但参数2的类型为'double'[-Wformat]

据我所知,参数是浮点型 . 是什么赋予了?也可以随意指出其他任何事情,我确信我的代码很草率 .

#include <stdio.h>
#include <math.h>

void findN (float PV, float FV, float interest)
{
float iDec = interest / 100;
float onePlusI = iDec + 1;
float leftSide = FV / PV;
float logOne = log(leftSide);
float logTwo = log(onePlusI);
float N = logOne / logTwo;
printf("%f\n", N);
}

void findI (float PV, float FV, float N)
{
float leftSide = FV / PV;
float expN = 1 / N;
float iPlusOne = pow(leftSide, expN);
float iDec = iPlusOne - 1;
float interest = iPlusOne * 100;
printf("%f\n", interest);
}

main ( )
{
int userInput;
printf("Press 1 to find Present Value, 2 to find Future Value, 3 to find Interest, or 4 to find Number of Periods\n");
scanf("%d", &userInput);
if (userInput = 3)
    {
    float Pres3;
    float Fut3;
    float Num3;
    printf("Enter Present Value\n");
    scanf("%f", Pres3);
    printf("Enter Future Value\n");
    scanf("%f", &Fut3);
    printf("Enter the Number of Periods\n");
    scanf("%f", &Num3);
    findN(Pres3, Fut3, Num3);
    }

else if (userInput = 4)
    {
    float Pres4;
    float Fut4;
    float Int4;
    printf("Enter Present Value\n");
    scanf("%f", Pres4);
    printf("Enter Future Value\n");
    scanf("%f", &Fut4);
    printf("Enter interest\n");
    scanf("%f", &Int4);
    findN(Pres4, Fut4, Int4);
    }
}

2 回答

  • 2
    if (userInput = 3)
    

    这是错误的,在这里你不再比较值 3 ,你将值 3 赋值给 userInput . 使用等于运算符 == 而不是 = 赋值运算符 .

    然后:

    scanf("%f", Pres3);
    

    你必须将指针传递给 Pres3 . 使用:

    scanf("%f", &Pres3);
    

    代替 .

    这两个问题在您的程序的其他地方重复出现 .

    最后, main() 不是在C中声明 main 的有效方法 . 使用 int main(void) .

  • 2

    你写了 scanf("%f", Pres3); 而不是 scanf("%f", &Pres3); . 它's complaining about the fact that the argument isn'是一个指针 .

    floatdouble 之间的混淆可能是因为你所在的机器 floatdouble 相同 .

相关问题