Thread: interest formula

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

    interest formula

    good evening all, im in the process of writing a code that will calculate loan payments. I made the equation for the interest formula, but when I compile it, I get the error:
    "called object is not a function"

    Code:
    void calc_loan(char*loan_amount, char*apr, char*loan_term, float *payment)
    {
         
         float i = (*apr/12);
         float x = (1+i);
         *payment = *loan_amount(i ((pow((x), *loan_term)))/((pow((x), *loan_term) -1))); 
        
         }
    I tried to simplify the equation as much as I could, and from what i can tell it should be right. Anyone see something I missed? Thanks in advance.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    loan_amount(
    this is function notation, but loan_amount is C-string, what do you want to do with it?
    also

    pow(x, *loan_term)
    x is double and *loan_term is one char are you sure it is what you want to do?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > *loan_amount(
    There is no operator between this variable and the (, so the compiler thinks it is a function call.

    Also, a char isn't large enough for storing the kinds of numeric values like a loan amount.
    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.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    Well its supposed to work where the user is asked to input a bunch of information, of which include loan amount. Then I want the loan amount, apr, and loan term that the user inputs to be plugged into this interest formula. Here is the entire code to give you an idea of what im going for:

    Code:
    void calc_loan(char*loan_amount, char*apr, char*loan_term, float *payment)
    {
         
         float i = (*apr/12);
         float x = (1+i);
         *payment = *loan_amount*(i* ((pow((x), *loan_term)))/((pow((x), *loan_term) -1))); 
        
         }
    Last edited by cboard; 03-28-2007 at 03:21 PM.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    Quote Originally Posted by Salem View Post
    > *loan_amount(
    There is no operator between this variable and the (, so the compiler thinks it is a function call.

    Also, a char isn't large enough for storing the kinds of numeric values like a loan amount.
    So I should change it to float*loan_amount?

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    I suggest you read how to get a number from the user in C first, then you should change loan_amount to a double (or a float but you will lose precision).

    Also, please be aware that, if you have an array (a C string in your case), then the name of the array is already a pointer to the first element. In that case, either is correct:

    Code:
    char myString[128];
    myFunctionCall(myString);
    
    // or
    
    myFunctionCall(&myString[0]);
    
    // or
    
    myFunctionCall(&(*myString));
    Please be aware that the two last notations are the least common ones and you should adapt your code to increase readability.

    Furthermore, it is very good that you are using fgets to read a string from the user, you could improve your code though by checking the return value from fgets. As explained here, you can check if fgets executed correctly and you should also test and remove the newline character:

    Code:
      char buf[BUFSIZ];
      char *p;
      
      printf ("Please enter a line of text, max %d characters\n", sizeof(buf));
      
      if (fgets(buf, sizeof(buf), stdin) != NULL)
      {
        printf ("Thank you, you entered >%s<\n", buf);
        
        /*
         *  Now test for, and remove that newline character
         */
        if ((p = strchr(buf, '\n')) != NULL)
          *p = '\0';
          
        printf ("And now it's >%s<\n", buf);
      }
    You can even put it all within one function:

    Code:
    int getString(char buf[], size_t bufSize)
    {
      char *p;
      
      printf ("Please enter a line of text, max %d characters\n", bufSize);
      
      if (fgets(buf, bufSize, stdin) != NULL)
      {
        if ((p = strchr(buf, '\n')) != NULL)
          *p = '\0';
        return 0;
      }
      return 1;
    }
    The function takes as argument the C-String and the size of that string (since sizeof within the function would return the size of the pointer only) and it returns 0 if it could read a string and 1 in the other case.
    Last edited by KONI; 03-28-2007 at 12:53 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fgets(line, 500, stdin);
    > sscanf(line, "&#37;s", loan_amount);
    > printf("\n");
    Yes, you probably need some kind of numeric data types for these (the rest seem fine as strings).

    So perhaps the parameter would be
    double *loan_amount

    And to convert it
    sscanf( line, "%lf", loan_amount );

    You should check the return result of sscanf, and in particular, check that the %s conversions really will fit into the intended strings.


    > char last_name[26], first_name[26], address[26], city_state[26];
    > char phone_number[9], account_number[9], loan_amount[9], apr[9],
    > loan_term[9];

    > information (&*account_number, &*last_name, &*first_name, &*address,
    > &*city_state, &*phone_number, &*loan_amount, &*apr, &*loan_term);
    Too many & and *
    Say you want the first 6 as strings, and the last 3 as doubles.
    Code:
        char last_name[26], first_name[26], address[26], city_state[26];
        char phone_number[9], account_number[9], 
        double loan_amount, apr, loan_term;
        
        information (account_number, last_name, first_name, address, 
            city_state, phone_number, &loan_amount, &apr, &loan_term);
    You only need the array name to pass an array to a function. The "&*" is the same, but they just cancel each other out.

    The prototype for the function being
    Code:
    void information(char *account_number, char *last_name, char *first_name,
        char *address, char *city_state, char *phone_number, double *loan_amount,
        double *apr, double *loan_term)
    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.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    Thanks again for all the input, I took all your advice and modified my code a bit. Last thing I need it to do is print out each month and the interent, principal and remaining balance to go along with it. And the all the inputs for loan_amount, apr, and loan_term are being printed out at the end. Perhaps this is where the problem lies?
    Last edited by cboard; 03-29-2007 at 12:24 PM. Reason: changed code

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    Im looking for an output similar to this:

    Customer #: 0000001234
    John Doe
    205 E 5th Ave
    New York, New York
    (563)563-5639
    -------------------------------------------------------------------------
    Loan Amount: $10000.00 APR: 120.00% Period: 24
    -------------------------------------------------------------------------
    Pay # Payment Principal Paid Interest Paid Remaining Balance
    1 1113.00 113.00 1000.00 9887.00
    2 1113.00 124.30 988.70 9762.70
    .
    .
    .
    23 1113.00 919.83 193.16 1011.82
    24 1113.00 1011.82 101.18 0.00
    -------------------------------------------------------------------------
    Customer Totals:
    Total Paid: $26711.95 Interest Paid: $16711.95

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Call calc_loan inside your for loop which prints each payment
    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.

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    Okay heres what I did now:

    Code:
    for(n=0;n<=loan_term;n++)
        {
        if(n%12==0)
        { k++;
        printf("%4d\t%10.2f\t  %6.2f\t   %10.2f\t   %10.2f\n",loan_term, payment, *ATI,*ATP,*RP);
        }
        }
    It seems to be working, but Ill im getting is zeros because the loan_term, interest, and loan_amount are being recorded correctly. At the end where it displays them, all I get is zeros for all of the instead of the actual numbers the user puts in..

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're still not calling calc_loan inside the loop.

    > printf("%4d\t%10.2f\t %6.2f\t %10.2f\t %10.2f\n",loan_term, payment, *ATI,*ATP,*RP);
    To print a float, drop the *'s on ATI etc.
    You have to be very careful about passing parameters to printf/scanf functions because there is absolutely no error checking.

    If you're using gcc, then you can add
    -W -Wall -ansi -pedantic
    to the compiler options, and that will diagnose many typical programming problems, including printf/scanf parameter mis-matches.
    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.

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    Okay I got it to work and I got the format down and everything, but it doesnt update the payments as the month increases?

    Code:
        
        for(n=1;n<=loan_term;n++)
        {
        calc_loan (&loan_amount, &apr, &loan_term, &payment, &ATI, &ATP, &RP);
        printf("&#37;4d\t%10.2f\t  %6.2f\t   %10.2f\t   %10.2f\n", n, payment, ATI, ATP, RP);
        }
    Last edited by cboard; 03-29-2007 at 10:33 PM.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to adjust the loan amount inside the loop as well
    Perhaps
    loan_amount -= ATP;
    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.

  15. #15
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    Ah that got it working, thank you sir.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming project help?
    By Debbie Bremer in forum C++ Programming
    Replies: 21
    Last Post: 10-11-2008, 02:48 AM
  2. creating and using functions
    By moy18 in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 09:09 PM
  3. Compound Interest Formula
    By veronicak5678 in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2007, 05:16 PM
  4. Loan calculation formula
    By C++Nerd in forum C++ Programming
    Replies: 1
    Last Post: 10-06-2002, 12:57 PM