gpt4 book ai didi

c - 如何保持运行总计

转载 作者:行者123 更新时间:2023-11-30 20:30:50 25 4
gpt4 key购买 nike

here is the picture of result of this program/我的问题是如何保持循环中的运行总计?因为在我的程序中,总债务仅基于循环终止之前用户的最后输入,而不是从所有债权人获取总债务

/This program is for calculating the total debt
#include <stdio.h>
#include <string.h>

int main()
{
float cb,ir,si,totaldebt; //local declaration
int time,i,sum=0;
char c[25];
printf("------------Welcome to Debt Management System-------------");
printf("\nNOTE:\nDear users,\n\tYou have to enter the creditor's name if you have a debt. But if you have nothing debt left, just enter none");
for (i=1;i>=1;i++)
{
printf("\n%d)Name of the creditor: ",i);
//the user inputs the name of the creditor
scanf("%s", c);
if (strcmp(c, "none") == 0) // condition wherein if the user inputs "none" in the name of the creditor, the loop will terminate
{
break;
}
printf("Enter your current balance: ");//the user inputs current balance from the said creditor
scanf("%f",&cb);
printf("Enter its interest rate: "); //the user inputs the interest rate of of the debt
scanf("%f",&ir);
printf("Enter time for the loan: ");//the user inputs month
scanf("%d",&time);
si=cb*ir*time/100;//simple interest
totaldebt=si+cb; //simple interest + current balance
sum+=totaldebt;
}
printf("The total balance you have for now is: %.2f\n",totaldebt); //
if (totaldebt<=5000)
{
printf("\nCongratulations!\n You only have %.2f debt left\n",totaldebt);
}
else
{
printf("\nWork harder because out of DEBT is out of DANGER\n");
}
return 0;
}

最佳答案

您已经在 sum 中跟踪运行总计。你只需要使用它:

float sum = 0;
...

printf("The total balance you have for now is: %.2f\n",sum ); //
if (sum <=5000)
{
printf("\nCongratulations!\n You only have %.2f debt left\n",sum );
}
else
{
printf("\nWork harder because out of DEBT is out of DANGER\n");
}

关于c - 如何保持运行总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53193229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com