【问题标题】:Codeblocks project.exe has stopped working CCodeblocks project.exe 已停止工作 C
【发布时间】:2013-04-06 18:36:19
【问题描述】:

每次我构建和运行我的项目文件时,一旦我与之交互,它就会崩溃。

#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;
}

非常感谢任何帮助

【问题讨论】:

  • 很遗憾scanf 无法确保正确的类型。
  • ab 都未初始化时,为什么会有complexnumber = a + b * j 行?当您说“当我与它互动时”时,您具体指的是哪一点?你输入的是什么值?

标签: c exe codeblocks


【解决方案1】:
scanf ("%f" , a);

scanf 需要一个指向它应该填充的变量的指针,所以它必须是

scanf ("%f" , &a);

b 也是如此。

【讨论】:

    【解决方案2】:

    包含头文件&lt;math.h&gt; 可能是个好主意。

    您的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 都未初始化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 2016-04-26
      相关资源
      最近更新 更多