Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,615 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 986 people online right now. Registration is fast and FREE... Join Now!




Looping in C

 
Reply to this topicStart new topic

Looping in C

yongone
post 15 Jul, 2008 - 08:03 PM
Post #1


New D.I.C Head

*
Joined: 7 Jul, 2008
Posts: 5

CODE

#include <stdio.h>

int
main(void)
{
  double princ_amt;    /* Principle amount borrowed */
  double airate;       /* Annual interest rate */
  double mirate;       /* Monthly interest rate */
  double m_int;        /* Monthly interest */
  double m_pay;        /* Monthly payments */
  double remain_amt;   /* Remaining after paying interest */
  double tot_int;      /* Total intrest on loan */
  int month;           /* How many months it will take to complete payment */

  /* Get principle amount */
  printf("Enter a principle amount:  ");
  scanf("%lf", &princ_amt);

  /* Gets annual interest rate */
  printf("Enter an annual interest rate: ");
  scanf("%lf", &airate);

  /* Converts annual interest to monthly */
  mirate = airate / 1200;

  /* Computes monthly interest amount */
  m_int = mirate * princ_amt;

  /* Shows user the amount of the first months intrest */
  printf("Your first month of intrest will be: %.2f\n", m_int);

  /* Gets monthly payment */
  printf("Enter the amount of your monthly payment: ");

  /* Shows how much the user borrowed */
  printf("You borrowed $%.2f\n", princ_amt);
  scanf("%lf", &princ_amt);

  /* Loops until monthly payments are larger than amount left */
  month = 0;
  tot_int = 0;
  m_pay = m_pay;
  while (princ_amt > m_pay)
{
    m_int = mirate * princ_amt; /* calculating monthly interest */
    remain_amt = m_pay - m_int; /* calculating remainder after paying interest */
    month = month + 1; /* calculating the months it take to complete */
    tot_int += m_int; /* calculating the full interest */
    princ_amt = princ_amt - remain_amt; /* paying the loan itself */

}
  printf("Your payment schedule consists of %d payments of $%.2f and a final prinicipal payment of $%.2f.\n", month, m_pay, princ_amt);

  printf("The total amount of interest you pay will be $%.2f\n", tot_int);

  return(0);
}


Ok my problem here is in the while loop.
It performs it. But it does not end by itself and i have to type in quit for the rest of the program to continue.
Please help me!!! Im sooo stuck.
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 15 Jul, 2008 - 08:08 PM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,351



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


You should take a look at the values it's creating... then you'll know why the loop doesn't end!

cpp

/* Loops until monthly payments are larger than amount left */
month = 0;
tot_int = 0;
m_pay = m_pay;
while (princ_amt > m_pay) {
m_int = mirate * princ_amt; /* calculating monthly interest */
remain_amt = m_pay - m_int; /* calculating remainder after paying interest */
month = month + 1; /* calculating the months it take to complete */
tot_int += m_int; /* calculating the full interest */
princ_amt = princ_amt - remain_amt; /* paying the loan itself */

printf("Pric_amt : %d\nm_pay : %d\n\n",princ_amt,m_pay);
}

QUOTE

Pric_amt : 698538988
m_pay : 1076740185

Pric_amt : -839326728
m_pay : 1076743434

Pric_amt : -590185847
m_pay : 1076746689

Pric_amt : 1484731370
m_pay : 1076749950

Pric_amt : 1129291981
m_pay : 1076753216

Pric_amt : -1617604935
m_pay : 1076756487

Pric_amt : 1872939124
m_pay : 1076759764

Pric_amt : -1244948879
m_pay : 1076763046

Pric_amt : 1952726842
m_pay : 1076766334

Pric_amt : -1379776546
m_pay : 1076769627

Pric_amt : 1681667164
m_pay : 1076772926

Pric_amt : -1708554221
m_pay : 1076776230

User is online!Profile CardPM

Go to the top of the page

yongone
post 15 Jul, 2008 - 09:02 PM
Post #3


New D.I.C Head

*
Joined: 7 Jul, 2008
Posts: 5

QUOTE(no2pencil @ 15 Jul, 2008 - 09:08 PM) *

You should take a look at the values it's creating... then you'll know why the loop doesn't end!

cpp

/* Loops until monthly payments are larger than amount left */
month = 0;
tot_int = 0;
m_pay = m_pay;
while (princ_amt > m_pay) {
m_int = mirate * princ_amt; /* calculating monthly interest */
remain_amt = m_pay - m_int; /* calculating remainder after paying interest */
month = month + 1; /* calculating the months it take to complete */
tot_int += m_int; /* calculating the full interest */
princ_amt = princ_amt - remain_amt; /* paying the loan itself */

printf("Pric_amt : %d\nm_pay : %d\n\n",princ_amt,m_pay);
}

QUOTE

Pric_amt : 698538988
m_pay : 1076740185

Pric_amt : -839326728
m_pay : 1076743434

Pric_amt : -590185847
m_pay : 1076746689

Pric_amt : 1484731370
m_pay : 1076749950

Pric_amt : 1129291981
m_pay : 1076753216

Pric_amt : -1617604935
m_pay : 1076756487

Pric_amt : 1872939124
m_pay : 1076759764

Pric_amt : -1244948879
m_pay : 1076763046

Pric_amt : 1952726842
m_pay : 1076766334

Pric_amt : -1379776546
m_pay : 1076769627

Pric_amt : 1681667164
m_pay : 1076772926

Pric_amt : -1708554221
m_pay : 1076776230




got it thanks. didnt notice.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 02:59AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month