c代码不适用于带有浮点数的turbo c ++

c code doesnt work on turbo c++ with the float numbers

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

int main(void)
{
    double r,d,x,y,pi;

    clrscr();
    printf("Input the value of r and degree: ");
    //scanf("%f",&r);
    //scanf("%f",&d);

    r = 12;
    d = 195;
    pi = 3.14;
    x = r * cos(d * pi/180);
    y = r * sin(d * pi/180);

    printf("The polar form is: (%f,%f)", x, y);
    getch();
}

在定义了 r 和 d 值的第一种情况下,输出是正确的,但在第二种情况下,当我给出输入时,输出与原始答案不匹配。该代码适用于代码块,但不适用于 Turbo C++。

我在 Turbo C++ 中做错了什么?

在:

//scanf("%f",r);
//scanf("%f",d);

您需要传递变量的地址,&r 和 &d。

格式说明符不匹配。使用 "%lf"double *

double r,d,x,y,pi;
...
//scanf("%f",&r);
//scanf("%f",&d);
scanf("%lf",&r);
scanf("%lf",&d);

更好的代码在使用前检查是否成功 r

if (scanf("%lf",&r) != 1) Handle_Error();

输出是笛卡尔坐标。 。我还推荐 '\n'.

// printf("The polar form is: (%f,%f)", x, y);
printf("The Cartesian form is: (%f,%f)\n", x, y);

打印double时出现Turbo-Crequires"l",所以用

printf("The Cartesian form is: (%lf,%lf)\n", x, y);

没有理由使用低精度 pi。建议

pi = acos(-1.0);