Thread: Loop Problem

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    3

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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    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);
      }
    }
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if(statement='A')
    Does this look like == ?

    Also, main returns an int, not void
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    I have changed both of those, and it still isn't working correctly. Sorry, I just need some help!!!!!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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 */
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM