C计算错误

时间:2015-03-09 21:42:35

标签: c

我正在尝试编写一个程序来计算一个简单电路的各种值,但没有一个计算能提供正确的答案。这就是问题所在:

void input();
void current(double,double,double);
void voltage1(double,double,double);

void input()
{
    double voltage;
    double R1;
    double R2;
    int selection;

    //I'm not entirely sure why the & signs need to be where they are, but the program won't run past here without them.
    scanf("%d",voltage);
    scanf("%d",&R1);
    scanf("%d",&R2);
    selection = menu(); //menu is a separate method that does work for selecting what to calculate.

    switch (selection)
    {
    case 1:
        current(voltage,R1,R2);
        break;
    case 2:
        voltage1(voltage,R1,R2);
        break;
    } //end of switch selection
} //end of input

void current(double voltage,double R1,double R2)
{
    double I = voltage / (R1 + R2);
    printf("%d",I);
} //end of current

void voltage1(double voltage,double R1,double R2)
{
    double V1 = (voltage * R1) / (R1 + R2)
    printf("%d",V1);
} //end of voltage1

使用10作为电压,R1和R2应该返回1表示电流,5表示电压,但我将始终得到789577626表示电流,0表示电压。电流总是在10,5和5的值附近,电压1总是为0。

1 个答案:

答案 0 :(得分:3)

您的scanfprintf格式说明符与您使用的类型不匹配。 %d适用于signed int类型,但您使用的是double。您需要使用%a%A%e%E%f%F%g或{{1 }}