2

私の仕事は、100 万ドルのジャックポットを獲得した誰かを銀行に預け、年利 8% を稼ぐことです。現在、毎年年末までに彼は 100,000 を引きます。それで、彼が口座を空にするのに何年かかるでしょうか?

これは私のコードです:

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

エラーは次のとおりです。プログラムは実行を続けますが、出力が得られません..

私は何を間違っていますか?

4

1 に答える 1

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

条件は常に true であるため、これは無限ループです。おそらく変更したいでしょうjackpot。何かのようなもの:

for (years = 0; jackpot >= 0; years++) 
    jackpot = year_ended(jackpot); 
于 2013-01-27T12:15:06.170 に答える