-
Loop Problem
I am a newbie to any kind of programming and I am trying to create a loan calculator, where I need to show the amount outstanding after each year, after I have made payments. I have worked out the subtracting the outstanding amount from the payment, but I can't stop it!!! Any help would be much appreciated. Thanks in advance.
Code:
int a,j,r;
float b,c,d,g,l;
double i,h,e,k,m,n,p;
char f;
/*Load Pre-Requisites*/
printf("Principal \n\r");
scanf("%d",&a);
printf("months \n\r");
scanf("%f",&c);
printf("deposit \n\r");
scanf("%d",&j);
printf("APR\n\r");
scanf("%f",&d);
b=c/12;
printf("Years %.1f \n\r",b);
g=d/100;
k=a-j;
printf("Mortgaged amount %f \n\r",k);
g++;
h=pow(g,b);
i=h*k;
printf("The total amount payable is %.2f \n\r",i);
e=i/c;
printf("The minimum monthly payment is %f \n\n\r",e);
printf("Do you require a Monthly or Annual statement (M or A) \n\r");
scanf("%d",&f);
if(f='A')
{
printf("Annual Mortgage Statement: \n\n\r");
for (l=1;l<=b;l++)
{
n=e*12;
for (p=i,r=1;r=1;r++,p=p-n)
{
printf("Year %.0f Payments of %.2f Balance %.2f \n\r",l,n,p);
}
}
}
else if(f='M')
{
printf("Monthly Mortgage Statement: \n\n\r");
for (l=1;l<=b;l++)
{
for (m=1;m<13;m++)
printf("Year %.0f Month %.0f Payment %.2f Balance %.2f \n\r",l,m,e);
}
-
> int a,j,r;
> float b,c,d,g,l;
> double i,h,e,k,m,n,p;
> char f;
Pick better names - you're allowed more than 1 character for a variable name
Things like
int numOfYears;
double balance;
are much more readable.
For starters, it tells you whether the code is plausable or not simply from reading it.
> if(f='A')
> for (p=i,r=1;r=1;r++,p=p-n)
Examples of using = where == or != was probably intended.
-
I have tried putting == instead of =, but the program just stops.
Am I doing the right thing by using for loops? Thanks again.
Code:
void main()
{
int value,deposit;
float years,term,apr,rate,month;
double total,step,permonth,mort,paypermonth,payperyear,balance;
char statement;
printf("Please enter the value of the property \n\r");
scanf("%d",&value);
printf("Please enter the Term (in months) of the mortgage \n\r");
scanf("%f",&term);
printf("Please enter your initial payment (deposit) \n\r");
scanf("%d",&deposit);
printf("Please enter the interest rate (APR) \n\r");
scanf("%f",&apr);
years=term/12;
printf("Years %.1f \n\r",years);
rate=apr/100;
mort=value-deposit;
printf("The amount to be mortgaged is %.2f \n\r",mort);
rate++;
step=pow(rate,years);
total=step*mort;
printf("The total amount payable is %.2f \n\r",total);
permonth=total/term;
printf("The minimum amount you need to pay each month is %.2f \n\n\r",permonth);
/*keep a running count of payments and subtract that value from total amount to pay*/
printf("Do you require a Monthly or Annual statement? (M or A) \n\r");
scanf("%c",&statement);
if(statement='A')
{
printf("Annual Mortgage Statement: \n\n\r");
for (month=1;month<=years;month++)
{
payperyear=permonth*12;
for (balance=total;;balance=balance-payperyear)
{
printf("Year %.0f Payments of %.2f Balance %.2f \n\r",month,payperyear,balance);
}
}
}
if(statement='M')
{
printf("Monthly Mortgage Statement: \n\n\r");
for (month=1;month<=years;month++)
{
for (paypermonth=1;paypermonth<13;paypermonth++)
printf("Year %.0f Month %.0f Payment %.2f Balance %.2f \n\r",month,paypermonth,permonth);
}
}
}
-
> if(statement='A')
Does this look like == ?
Also, main returns an int, not void
-
I have changed both of those, and it still isn't working correctly. Sorry, I just need some help!!!!!
-
Some points (marked by !!) for you to think about.
Code:
#include <stdio.h>
#include <math.h>/*!! missing, needed for pow*/
int/*!!not void*/ main()
{
int value,deposit;
float years,term,apr,rate,month;
double total,step,permonth,mort,paypermonth,payperyear,balance;
char statement;
printf("Please enter the value of the property \n\r"); /*!! \r is not needed */
scanf("%d",&value);
printf("Please enter the Term (in months) of the mortgage \n\r");
scanf("%f",&term);
printf("Please enter your initial payment (deposit) \n\r");
scanf("%d",&deposit);
printf("Please enter the interest rate (APR) \n\r");
scanf("%f",&apr);
years=term/12;
printf("Years %.1f \n\r",years);
rate=apr/100;
mort=value-deposit;
printf("The amount to be mortgaged is %.2f \n\r",mort);
rate++;
step=pow(rate,years);
total=step*mort;
printf("The total amount payable is %.2f \n\r",total);
permonth=total/term;
printf("The minimum amount you need to pay each month is %.2f \n\n\r",permonth);
/*keep a running count of payments and subtract that value from total amount to pay*/
printf("Do you require a Monthly or Annual statement? (M or A) \n\r");
/*!! another scanf victim */
while ( getchar() != '\n' );
scanf("%c",&statement);
if(statement==/*!! ==, not =*/'A')
{
printf("Annual Mortgage Statement: \n\n\r");
/*!! month <= years makes no sense */
for (month=1;month<=years;month++)
{
payperyear=permonth*12;
/*!! this for loop has no exit condition */
/*!! also, do you even need a loop here for annual payments? */
for (balance=total;;balance=balance-payperyear)
{
printf("Year %.0f Payments of %.2f Balance %.2f \n\r",
month,payperyear,balance);
}
}
}
if(statement==/*!! ==, not =*/'M')
{
printf("Monthly Mortgage Statement: \n\n\r");
for (month=1;month<=years;month++)
{
for (paypermonth=1;paypermonth<13;paypermonth++)
/*!! foo.c:51: warning: too few arguments for format */
/*!! meaning you have more % conversions than parameters */
printf("Year %.0f Month %.0f Payment %.2f Balance %.2f\n\r",
month,paypermonth,permonth);
}
}
return 0;/*!! missing */
}