-2
  • Hi, I am trying to create a Loan payback program which allows the user to input the Loan Amount, APR%, Monthly payments and then a 'For Loop' to run the program until the Loan Amount has hit 0 (each Loop will minus the Monthly Payment) and then update the Current Loan.
  • The CurrentLoan will start off the same as the LoanAmount, then change by the amount of the monthly payment(s). It only prints out 1 line of the CurrentLoan code and stops, does anyone know why?

void main()
{
    float LoanAmount;
    float APR;
    float Monthly; /*Prompts Number to a Float characrter type*/
    float DailyInt;
    float OutstandingLoan;
    float CurrentLoan;
    int i, Day, Month, Year;

    i = 0;
    Day = 0; /* Day = 1 Day */
    Month = Day * 30; /* 30 days per month = Month * 30 days*/
    Year == Month * 12; /* 12 months per yar * 30 days per month */

    printf(" Please enter in the Loan Amount : \n\r"); /*prompts user to enter float*/
    scanf("%f", &LoanAmount);

    printf("Please enter in the interest rate as a precentage per year (APR) : \n\r");
    scanf("%f", &APR);

    printf("Please enter in the Monthly payment plan : \n\r");
    scanf("%f", &Monthly);

    printf("\nPerforming calculation using a loan of Loan Amount: %.2f \n\r", LoanAmount);
    printf("with a monthly payment of : %.0f \n\r", Monthly);
    printf("and an APR rate of : %.2f \n\r", APR);

    printf("\n Month \ | Year \ | Current Loan \ | Daily Interest \| total Interest\n\r");

    for (CurrentLoan = LoanAmount; CurrentLoan == 0; ++CurrentLoan); 
    {
        CurrentLoan += Monthly;
        printf("%.2f \n\r", CurrentLoan);               
    };
    return(0);  
}
Pablo
  • 13,271
  • 4
  • 39
  • 59
Morph
  • 17
  • 4

2 Answers2

1

To answer your specific question as to why it only prints once, it is because your print statement is not in a loop.

for (CurrentLoan = LoanAmount; CurrentLoan == 0; ++CurrentLoan); // <-- this semicolon is the problem

The semicolon there is separating your scoping brackets from the loop, so basically you loop through and do nothing, and then when done with the loop, you do your arithmetic and printing.

Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
  • Cheers man - this solved the issue with single line of code but now only outputs 0.00.. but outputs the lines it takes to get down to 0.. I just don't know what else it could be. – Morph Feb 21 '18 at 21:35
0

Several issues - beyond the year == day..:

  • The for loop initialises CurrentLoan to eg. 5000, and runs as long as CurrentLoan == 0, which is false in all cases, except when the user typed '0' to the Loan Amount question.
  • You're incrementing CurrentLoan with one (++CurrentLoan) in each iteration - why?
  • you have ';' before '{' in the for loop - causing the content of '{ .. }' after it to be considered a scope inside a function, rather than the actions of the loop. This means it will only execute once the for-loop has completed, and not with each iteration of the for loop.
  • Unless the user types in a negative Monthly value, the loan is increased and not reduced?
  • you're returning 0, but your main function is specified as void..

A working example (of the loop - I've not done the APR part, as it doesn't seem that this is your question):

int main(int argc, char** argv) {
    float LoanAmount;
    float Monthly; /*Prompts Number to a Float character type*/

    printf("Please enter in the Loan Amount : \n\r"); /*prompts user to enter float*/
    scanf("%f", &LoanAmount);

    printf("Please enter in the Monthly payment plan : \n\r");
    scanf("%f", &Monthly);

    printf("\nPerforming calculation using a loan of Loan Amount: %.2f \n\r", LoanAmount);
    printf("with a monthly payment of : %.0f \n\r", Monthly);

    for (; LoanAmount >= 0; LoanAmount -= Monthly) {
        printf("%.2f \n\r", LoanAmount);
    }
    return (0);
}

Changes:

  • Removed excess variables not needed
  • Decrease LoanAmount with Monthly in each iteration
  • print (remaining) LoanAmount in each iteration
  • for loop stops once at/below zero

Output:

Please enter in the Loan Amount : 
11111
Please enter in the Monthly payment plan : 
1949

Performing calculation using a loan of Loan Amount: 11111.00 
with a monthly payment of : 1949 
11111.00 
9162.00 
7213.00 
5264.00 
3315.00 
1366.00 
  • The '++CurrentLoan' i believed would of counted the amount of loops it took for it to hit '0' - although i am trying to incorporate a months counter (See how many months it takes to pay) Well i originally required the system to reduce the LoanAmount by whatever the MonthlyPayment is on a Monthly basis (I.e a monthly counter) - so i was a bit unsure on certain parts of my code. Thank you very much your help is greatly appreciated - i am not confident on Loops so i am taking the feedback to use for future references. – Morph Feb 21 '18 at 22:42