我的初学C代码已停止工作

时间:2013-09-19 08:03:03

标签: c

本学期我正在修读我的第一个编程课程,我无法弄清楚我的程序是怎么回事。我必须编写一个程序来计算这么多年后有兴趣的总金额。公式为y = p *(1 + r)^ n

无论如何,每当我运行我的代码时,它会显示为“ _ 已停止工作”并关闭。

这是我的代码:

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

int main (void)
{
    double p, r, n, y; 

    printf("Enter the interest rate>");
    scanf("%lf", r);
    printf("Enter the principle amount of money invested>");
    scanf("%lf", p);
    printf("Enter the number of years invested>");
    scanf("%lf", n);

    y = pow(p*(1+r),n);

    printf("The total amount of money is %f.\n", y);

    system("PAUSE");
    return (0);
}

我试过谷歌搜索它似乎可能与“初始化”有关,但我不确定这意味着什么或如何做到这一点。非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

首先,scanf()函数需要变量的地址而不是变量本身,所以它应该像scanf("%lf", &r);一样使用试试看,你会没事的

其次从不使用system("PAUSE") !! 它是特定于平台的(窗口),它是错误的system("pause"); - Why is it wrong?

您正在学习使用系统(PAUSE)以错误的方式进行编程,在上面的链接中您可以看到原因!

答案 1 :(得分:0)

此代码是在linux平台上编写和测试的。

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

int main (void)
{
    double p, r, n, y, value; 
    int a = 3, b = 2;

        printf("Enter the interest rate>");
        scanf("%lf", &r);
        printf("Enter the principle amount of money invested>");
        scanf("%lf", &p);
        printf("Enter the number of years invested>");
        scanf("%lf", &n);
    value = p * (r + 1);
    y = pow(value, n);
        printf("The total amount of money is %f.\n", y);

        //system("PAUSE"); 
        return (0);
}

在linux中编译此代码,

gcc code.c -lm

我不知道为什么,即使我添加#include,我也被迫在编译时包含-lm。任何人都可以随意更新答案。

更新

请参阅此答案,了解为何必须使用-lm Undefined reference to 'pow' even though -lm is a compile flag. [C]

相关问题