My little program... help pls!

This is a discussion on My little program... help pls! within the C++ Programming forums, part of the General Programming Boards category; Hi everyone! As i have left my programming module to the last minute, I have been programming now for a ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    21

    My little program... help pls!

    Hi everyone!

    As i have left my programming module to the last minute, I have been programming now for a few hours(literally) and I must say I am pretty damn good at it!


    This is a program I need to tackle, it is a basic loan repayment thing.

    I have corrected all the errors so now it runs, but how do i get it to stop on the remaining months?


    you'll see what i mean when you run it:


    Code:
    #include <stdio.h>
    #include <conio.h>
    
    		int main()
    
    
    	{
    
       	float loan, rate, payment;
          int month=0;
    
    
         printf ("Amount of loan		       =		");
         scanf("%f", & loan);
         printf("Monthly interest rate		=		");
         scanf("%f", & rate);
         printf("Payment per month			=		");
         scanf("%f", & payment);
    
    
         while (loan>0.0)
         {
         month++;
         	loan=loan+(loan*rate/100);
          loan=loan-payment;
          printf("In month %d the sum remaining is =  %f\n", month, loan);
         }
         printf("It will take %d months to pay\n", month);
         getch();
    
    
       }

  2. #2
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    Take a look at what your loop does. Say you enter the following data.

    loan = 3.33
    rate = 3.1
    payment = 0.33

    (I like three's)

    So on month twelve, loan will equal 0.093329. The next iteration of the loop will begin and since 0.093329 is greater than 0 it will perform the calculations again and print a negative value (in this case, ~-0.233). What you want to do is check to make sure that your loan is greater than 0 BEFORE you make your calculations, instead of afterwards.

    What you could do is add a little if statement in your loop. Something like...

    Code:
    if(loan <= 0.0f)
      printf("You have completed your loan payments! FREEDOM!\n");
    else
      printf("In month %d the sum remaining is =  %f\n", month, loan);

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    >What you want to do is check to make sure that your loan is greater than 0 BEFORE you make your calculations

    I echo that.

    >I have been programming now for a few hours(literally)

    Try going for 48 or 72 hours without stopping except for sleeping and eating

    About your code. I used to use conio.h as well, but it is a non-standard header file, so you might want to not include that file. Instead of using getch(), which is a function inside conio.h, you can use getc() or getchar() in stdio.h.

    Oh....and remember to return 0 at the end of your main function.
    My Website

    "Circular logic is good because it is."

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Indentation - why is int main the most indented line of the program?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    You also should think about the case when the payment is not enough to pay the loan...
    You should somehow prevent the infinite loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    21

    cheers

    an IF statement solves it very easily indeed.

    cheers guys

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by DavidP
    Oh....and remember to return 0 at the end of your main function.
    If main() gets to the closing brace and there's no explicit return statement, it returns 0 automatically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  2. newbie here.. pls help me in this program!
    By rothj0hn in forum C Programming
    Replies: 2
    Last Post: 02-01-2006, 09:40 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Pls debug my scroll program
    By Raison in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2004, 07:30 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21