2013-04-06 76 views
1

每次构建并运行我的项目文件时,一旦与其交互,就会崩溃。Codeblocks project.exe已停止工作C

#include <stdio.h> 

int main() 
{ 
    float complexnumber, a, b, r, j, theta; 

    j = -1; 
    complexnumber = a+b*j; 

    printf ("Please enter intput A and B in the form of a+bj\n"); 

    printf ("Input A:"); 
    scanf ("%f" , a); 

    printf ("Input B:"); 
    scanf ("%f" , b); 

    theta = atan (a/b); 
    printf ("Theta=\n" , theta); 

    r = sqrt (pow(a, 2) + pow(b , 2)); 
    printf ("R=\n" , r); 

    return 0; 
} 

任何帮助深表感谢

+0

这是一个耻辱'scanf'不能确保适当的类型。 – chris 2013-04-06 18:38:34

+2

当'a'和'b'都未初始化时,为什么你有'complexnumber = a + b * j'这行?当你说“当我与它互动”时,你具体指的是哪一点?你输入什么值? – 2013-04-06 18:41:10

回答

1
scanf ("%f" , a); 

scanf需要一个指针变量应当填写,这样就必须

scanf ("%f" , &a); 

,类似的还有b

1

包含头文件<math.h>可能是一个好主意。

printf()声明

printf ("Theta=\n" , theta); 

看起来不正确,

它应该是,

printf ("Theta=%f\n" , theta); 

同样,

printf ("R=%f\n" , r); 

scanf()说法也是错误的,应该是

scanf("%f",&a); 

线complexnumber = a+b*j;将垃圾值赋给complexnumber既是ab是未初始化的。