# Thread: Loan Calculator (problem with function & pointer)

1. ## Loan Calculator (problem with function & pointer)

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

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..
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 ;

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. 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. Also when you say

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

you have an array , and in fact the semi colon is missing. 4. ok.. so simple one first..
how am i going to correct this "status == ELIGIBLE "?
is it... status == [ELIGIBLE]? 5. Originally Posted by naspek 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;

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.

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

It should be
scanf("%f", &S); 6. 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'.... 7. 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? 8. how are you calling this function from the main? Can you post the code for that as well? 9. Originally Posted by roaan 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. 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. 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("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. 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. 13. 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. 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. Originally Posted by tabstop 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..
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 