как сохранить промежуточный итог - PullRequest
0 голосов
/ 07 ноября 2018

вот изображение результата этой программы / Мой вопрос о том, как сохранить итоговую сумму в цикле? потому что в моей программе общая задолженность будет основываться только на последнем вводе пользователя перед завершением цикла, вместо этого получая общую задолженность от всех кредиторов

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

1 Ответ

0 голосов
/ 07 ноября 2018

Вы уже отслеживаете промежуточный итог в 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");
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...