不能打印出我的功能计算

时间:2013-01-27 12:10:35

标签: c

我的任务是拿一些赢得100万美元累积奖金的人,把它放入银行并以每年8%的利率赚钱。现在,在每年结束之前,他会挣100k。那么多久他需要清空他的账户?

这是我的代码:

#include <stdio.h>
long int year_ended(long int prize);//declare the function to be implemented 


int main(void)

{

    long int jackpot, years;

    jackpot = 1000000;

    for (years = 0; jackpot >= 0; years++) //counting the years until the jackpot is 0
        year_ended(jackpot); 

    printf("it will take %ld years to empty the account", years);

    return 0;
}

long int year_ended(long int prize) //function to decrement 100k every year and add the erned interest

{
    double yearlyRate = 0.08;

    int YearlyWithdraws = 100000, left;

    left = (prize - YearlyWithdraws) * yearlyRate + (prize - YearlyWithdraws);

    prize = left;

    return prize;
}

错误是:程序一直在运行,但我没有输出..

我做错了什么?

1 个答案:

答案 0 :(得分:3)

for (years = 0; jackpot >= 0; years++)
    year_ended(jackpot); 

这是一个无限循环,因为条件始终为真。您可能想要修改jackpot。类似的东西:

for (years = 0; jackpot >= 0; years++) 
    jackpot = year_ended(jackpot);