Thread: Loan Calculator (problem with function & pointer)

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38

    Loan Calculator (problem with function & pointer)

    hey there...
    need ur guys help to highlight my mistakes..
    i'm suppose to get the output below...

    LOAN ADVISOR
    Car purchase price/loan <RM>: 15000
    Down payment <RM>:0
    Payment period <in year>:7
    Monthly salary:1500

    Monthly payment <RM>:228.57
    Status:ELIGIBLE


    but, i cant get it..
    please help me to improve my program..
    i dont know where is my mistake..

    Code:
    #include <stdio.h>
    void loan_calculate (float L, float DP, int Y, float S, float *monthlyPtr, char status [], float I);
    int main ()
    {
     float L, DP, S, I;
     int Y;
     char status [20];
    
     printf("LOAN ADVISOR\n");
     printf("Car purchase price/loan <RM>: \n");
     scanf("%f", &L);
    
     if (L < 50000)
      I = 4.0%;
     else if (L < 100000 && L >= 50000)
      I = 3.5%;
     else if (L >= 100000)
      I = 3.0%;
    
     printf("Down payment <RM>: \n");
     scanf("%f", &DP);
     printf("Payment period <in year>: \n");
     scanf("%d", &Y);
     printf("Monthly salary: \n");
     scanf("%f", S);
    
     loan_calculate (&monthlyPtr, &status);
     printf("Monthly payment <RM>: %f\n", monthlyPtr);
     printf("Status: %s\n", status);
     return 0;
    }
    
    void loan_calculate (float L, float DP, int Y, float S, float *monthlyPtr, char status [], float I){
    
     *monthlyPtr = ((L-DP) + ((L-DP) * (I/100) * Y)) / (12 * Y);
    
     if (*monthlyPtr <= ((25/100)*S))
     status == ELIGIBLE
    
     else
     status == NOT ELIGIBLE
     return;
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    The prototype for the function that you declare is

    [insert]
    Code:
    void loan_calculate (float L, float DP, int Y, float S, float *monthlyPtr, char status [], float I);
    but the way you call your function is

    [insert]
    Code:
    loan_calculate (&monthlyPtr, &status);
    Just make sure you pass the right number of arguments. This is one of the mistakes.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Also when you say

    [insert]
    Code:
    status == ELIGIBLE // this is wrong

    you have an array , and in fact the semi colon is missing.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    ok.. so simple one first..
    how am i going to correct this "status == ELIGIBLE "?
    is it... status == [ELIGIBLE]?

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by naspek View Post
    ok.. so simple one first..
    how am i going to correct this "status == ELIGIBLE "?
    is it... status == [ELIGIBLE]?
    See what happens is that when you declare an array as you have done above

    char status[20];

    Now you cant declare the entire array at once like the way you are doing. Array elements can only be initialized one at a time. Also you tend to use the == operator which is used for comparison and not for assignment. If you want to assign ELIGIBLE or NOTELIGIBLE in your program i would suggest make use of a char pointer instead.

    Also in your program

    [insert]
    Code:
    printf("Monthly salary: \n");
     scanf("%f", S);
    Where is the & operator for S??????

    It should be
    scanf("%f", &S);

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    ok.. i've made corrections d...

    Code:
    void loan_calculate (float L, float DP, int Y, float S, float *monthlyPtr, char *statusPtr){
    
     *monthlyPtr = ((L-DP) + ((L-DP) * (I/100) * Y)) / (12 * Y);
    
     if (*monthlyPtr <= ((25/100)*S))
     *statusPtr = "ELIGIBLE";
    
     else
     *statusPtr = "NOT ELIGIBLE";
     return;
    }
    however, the orror said that i haven't declare 'L', 'DP', 'Y', 'I'....
    Last edited by naspek; 09-01-2009 at 01:35 AM.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    First off, use descriptive variable names. Names should be at least three characters, excepting a few specific cases, such as indexing variables i, j, and k.

    Second, you don't have a variable called I in that function. So that's why it's complaining about that. Are the other errors on that line, or from elsewhere?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    how are you calling this function from the main? Can you post the code for that as well?

  9. #9
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    Quote Originally Posted by roaan View Post
    how are you calling this function from the main? Can you post the code for that as well?
    loan_calculate (L, DP Y, S, &monthlyPtr, &status);

  10. #10
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    As King Mir pointed out since you dont have variable called I in the function thats why its complaining.

    Also when you get the interest rates you do
    if (L < 50000)
    I = 4.0%;
    else if (L < 100000 && L >= 50000)
    I = 3.5%;
    else if (L >= 100000)
    I = 3.0%;

    What is the % sign doing here? Also I is an integer so why not assign it the value the integer way like
    I = 4;

    Plus make the variables name more specific so that you also feel easy to understand and debug.

    And regarding the erros for D, P, Y where are the line numbers where these are occuring.

  11. #11
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    ok.. this is the code that i've makes some corrections..
    Code:
    #include <stdio.h>
    void loan_calculate (float L, float DP, int Y, float S, float *monthlyPtr, char *statusPtr, float I);
    int main ()
    {
     float L, DP, S, I, monthly;
     int Y;
    
     printf("LOAN ADVISOR\n");
     printf("Car purchase price/loan <RM>: \n");
     scanf("%f", &L);
    
     if (L < 50000)
      I == 4.0;
     else if (L < 100000 && L >= 50000)
      I == 3.5;
     else if (L >= 100000)
      I == 3.0;
    
     printf("Down payment <RM>: \n");
     scanf("%f", &DP);
     printf("Payment period <in year>: \n");
     scanf("%d", &Y);
     printf("Monthly salary: \n");
     scanf("%f", &S);
    
     loan_calculate (L, DP, Y, S, &monthlyPtr, &status);
     printf("Monthly payment <RM>: %f\n", monthly);
     printf("Status: %s\n", &status);
     return 0;
    }
    
    void loan_calculate (float L, float DP, int Y, float S, float *monthlyPtr, char *statusPtr, float I){
    
     *monthlyPtr = ((L-DP) + ((L-DP) * (I/100) * Y)) / (12 * Y);
    
     if (*monthlyPtr <= ((25/100)*S))
     *statusPtr = "ELIGIBLE";
    
     else
     *statusPtr = "NOT ELIGIBLE";
     return;
    }
    and.. this are the errors that occur..

    Code:
    a.c: In function `main':
    a.c:26: error: `monthlyPtr' undeclared (first use in this function)
    a.c:26: error: (Each undeclared identifier is reported only once
    a.c:26: error: for each function it appears in.)
    a.c:26: error: `status' undeclared (first use in this function)
    a.c:26: error: too few arguments to function `loan_calculate'
    a.c: In function `loan_calculate':
    a.c:37: warning: assignment makes integer from pointer without a cast
    a.c:40: warning: assignment makes integer from pointer without a cast

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Each of those errors should be easy to fix if you read them and look at your code.

    1. Look in your main function. Find where monthlyPtr is declared.

    2. Look in main function. Find where status is declared.

    3. Look in main function where you call loan_calculate. Count the number of parameters you are passing. Compare this with your loan_calculate function definition.
    Last edited by DaveH; 09-01-2009 at 07:45 AM.

  13. #13
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    what i'm trying to do is, i want to store E = ELIGIBLE and N = NOT ELIGIBLE.
    then.. i want to call them by referrence in main function..

    Code:
    char *statusPtr;
    
     loan_calculate(L, DP, Y, S, &monthly, status, I);
     printf("Monthly payment <RM>:%.2f", monthly);
     printf("Status:%s", status);
    
    return 0;
    }
    
    void loan_calculate (float L, float DP, int Y, float S, float *monthlyPtr, char *statusPtr, float I){
    
    *monthlyPtr = ((L - DP)+((L-DP)*(I/100)*Y))/(12*Y);
    
    if (*monthlyPtr <= ((25/100)*(*monthlyPtr)))
    statusPtr = 'E';
    E = ELIGIBLE;
    
    else
    statusPtr = 'N';
    N = NOT ELIGIBLE;
    
    return;
    }
    however.. these errors occur..
    a.c: In function `main':
    a.c:28: error: incompatible type for argument 6 of `loan_calculate'
    a.c: In function `loan_calculate':
    a.c:40: warning: assignment makes pointer from integer without a cast
    a.c:41: error: `E' undeclared (first use in this function)
    a.c:41: error: (Each undeclared identifier is reported only once
    a.c:41: error: for each function it appears in.)
    a.c:41: error: `ELIGIBLE' undeclared (first use in this function)
    a.c:43: error: syntax error before "else"
    a.c:45: error: `N' undeclared (first use in this function)
    a.c:45: error: `NOT' undeclared (first use in this function)
    a.c:45: error: syntax error before "ELIGI

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to store ELIGIBLE in E and NOT ELIGIBLE in N, then do so. But first you will have to have variables called E and N. The easiest way would probably be
    Code:
    char *E = "ELIGIBLE";
    char *N = "NOT ELIGIBLE";
    Of course, the question is then why you want such a thing, but only you could answer that.

  15. #15
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    Quote Originally Posted by tabstop View Post
    If you want to store ELIGIBLE in E and NOT ELIGIBLE in N, then do so. But first you will have to have variables called E and N. The easiest way would probably be
    Code:
    char *E = "ELIGIBLE";
    char *N = "NOT ELIGIBLE";
    Of course, the question is then why you want such a thing, but only you could answer that.
    ok.. in main function, i just need to print the output of
    Status: ELIGIBLE or..
    Status: NOT ELIGIBLE

    my idea is to declare all my works for status in function definition..
    and in the main function, i just wanna print it..
    i try to follow your advice..
    Code:
    if (*monthlyPtr <= ((25/100)*(*monthlyPtr))){
    statusPtr = 'E';
    char *E = "ELIGIBLE";
    }
    else{
    statusPtr = 'N';
    char *N = "NOT ELIGIBLE";
    }
    return;
    }
    but... Error occur:
    a.c: In function `main':
    a.c:26: warning: passing arg 6 of `loan_calculate' makes pointer from integer without a cast
    a.c: In function `loan_calculate':
    a.c:37: warning: assignment makes pointer from integer without a cast
    a.c:41: warning: assignment makes pointer from integer without a cast

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM