Thread: Monthly payment calculator not working out

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    12

    Monthly payment calculator not working out

    Hey again guys,

    I'm still learning here so bear with me. I'm trying to start this lightweight payment calculator, but it's giving me a problem when I enter data. After I scanf to principal, term and rate, if I printf to check the new values, the first two are correct. However, it shows the value of rate after the scanf to be 0.00 regardless of what number I enter. I suspect the cause will be obvious, but I'm just not seeing it. I'd appreciate a little help pinpointing it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    int main()
    {
    	double moPay, rate, principal, term;
    	int months;
    /*	- moPay is the monthly paymount amount to be calculated
    	- rate is the interest rate
    	- principal is the balance due before interest
    	- term is the number of years the payments will last
    	- months is the the term converted into months by multiplying by 12 
    */
    	
    	printf("\nEnter the amount of the loan using\ndecimal places (ex: $1000 is 1000.00): ");
    	scanf("%lf", &principal);
    	printf("\nNow enter the term of the loan in years: ");
    	scanf("%d", &term);
    	printf("\nEnter the interest rate for the loan (example: 5%% is 5): ");
    	scanf("%lf", &rate);
    
    
    	printf("\nPrincipal is %.2lf, term is %d, and rate is %lf.\n", principal,term,rate);
    	
    system("pause");
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > printf("\nPrincipal is %.2lf, term is %d, and rate is %lf.\n", principal,term,rate);
    Because %d is not what you use to print a floating point value.
    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
    Jun 2011
    Posts
    4,513
    Code:
    scanf("%d", &term);
    Same for reading a floating point value. Did you mean to declare "term" as an int?

  4. #4
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    Quote Originally Posted by Matticus View Post
    Code:
    scanf("%d", &term);
    Same for reading a floating point value. Did you mean to declare "term" as an int?
    Ahh, that's it. Yes I did intend for term to be an int. I gotta start paying better attention to these things. Thanks a lot guys.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Indeed you do. But even the best of us have slip-ups. That's why compiler warnings are so very important. For instance, compiling your code, I got:

    Code:
    main.c||In function 'main':|
    main.c|20|warning: format '%d' expects type 'int *', but argument 2 has type 'double *'|
    main.c|25|warning: format '%d' expects type 'int', but argument 3 has type 'double'|
    main.c|9|warning: unused variable 'months'|
    main.c|8|warning: unused variable 'moPay'|
    ||=== Build finished: 0 errors, 4 warnings ===|
    The first two warnings explicitly mention the type of mistake and the associated line number.

    So you should heed all compiler warnings. If you didn't see these while compiling, then you likely need to increase your compiler warnings (or, if you're using certain IDEs, don't "build and run", but just "build" to see the warnings).

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    @Matticus, thanks for the input, and I actually only got one "unused variable" warning originally. I've gotten a little more into it and am having issues with the payment calculation now. It's coming back with payments that are way too low (not even half what they should be). I'm also getting a different warning now about "call to 'pow' with no prototype in main". Any chance someone can point out where I'm dropping the ball on this?

    Here's the newest code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    
    
    int main()
    {
        double moPay, annRate, principal, rate, ratePow, totalPmt, totalInt; 
        int months, yrs, term; 
    /*    - moPay is the monthly paymount amount to be calculated
        - annRate is the annual interest rate
        - principal is the loan amount before interest
        - yrs is the number of years the loan is for
        - term is the life of the loan converted into months by multiplying yrs by 12 
    */
        /* user input of loan data */
        printf("\nEnter the amount of the loan: ");
        scanf("%lf", &principal);
        printf("\nNow enter the life of the loan in years: ");
        scanf("%d", &yrs);
        printf("\nEnter the annual interest rate for the loan (example: 5%% is 5): ");
        scanf("%lf", &annRate);
    
    
        /* loan and payment calculation */
        term=yrs  * 12;
        rate=annRate / 1200.0;
        moPay=(rate+rate / (pow (1.0+rate, term)-1.0)) * principal;    
        
        totalPmt=term * moPay;
        totalInt=totalPmt - principal;
        
        /* output loan and payment details */
        printf("\nPrincipal is %.2lf, term is %d months, and rate is %.0lf%%.\n", principal,term,annRate);
        printf("\nYour monthly payment is %.2lf.\n\nYour total interest is %.2lf.\n\nYour total payment is %.2lf.\n", moPay,totalInt,totalPmt);
        
        
        
    system("pause");
    return 0;
    }

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Where is the prototype for pow()? I suspect that it's in <math.h> (so #include <math.h>)

    Something else to consider is how C deals with a function that's called where there is no prototype for that function "scope"; i.e. in your code the C compiler has no idea what pow() returns or what parameters it expects because the prototype is in math.h which you haven't included. The answer is that the compiler makes some assumptions. First, it doesn't make any guess about the number of parameters at all (maybe pow() expects 5000 parameters, or maybe it expects none... the C compiler will not even attempt to guess this, it's can't). The second assumption that the compiler will make is that pow() returns an int. It just so happens that pow() does not return an int, but the compiler makes that assumption anyway just because there really isn't any better option. This has implications. Looking at this line:

    Code:
    moPay=(rate+rate / (pow (1.0+rate, term)-1.0)) * principal;
    The compiler has tried to make an educated guess that the pow() function returns an int. But, in reality, pow() returns a double. But because the compiler thinks it returns an int (due to the lack of a function prototype) the return value of pow() loses the decimal parts of the floating point number with obviously bad results.

  8. #8
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    @Hodor, thanks for that. Just including math.h fixed everything. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Calculator Not Working...What is wrong?
    By usernamed in forum C++ Programming
    Replies: 7
    Last Post: 08-08-2013, 10:52 PM
  2. Monthly Payment Program Help
    By sharingan_gv in forum C Programming
    Replies: 7
    Last Post: 07-14-2011, 09:07 AM
  3. calculate monthly payment
    By yacek in forum C Programming
    Replies: 2
    Last Post: 10-04-2010, 07:59 PM
  4. monthly payment from math to C++
    By summerwine in forum C++ Programming
    Replies: 4
    Last Post: 06-10-2010, 05:31 PM
  5. A Simple Calculator Program Not Working
    By oobootsy1 in forum C++ Programming
    Replies: 9
    Last Post: 01-09-2004, 09:34 PM