Function Problems

I wrote the code and ran it before i put it into function. I'm terrible at functions and so now i'm stuck, say the variables are not being initialized.
my code is as followed:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void CalcMP (float PR, float IM, float Q, float MP, float IY, float P, int NM);
void printinfo (float PR, float IY, float IM, int NY, int NM, float MP);
void printTable (int Month, float balance, float MP, float Interest, float total, int NM, float IM);



int main ()
{

	float total = 0;
	int Month = 1;
	float PR;
	float IY;
	int NY;
	int NM;
	float IM;
	float P;
	float Q;
	float MP;
	float Interest;
	float balance;
	float newbalance;

	printf("Amount of the loan (principal)?\t\t");
	scanf("%f", &PR);
	printf("Intrest rate per year (percent)\t\t");
	scanf("%f", &IY);
	printf("Number of years?\t\t\t");
	scanf("%d", &NY);


	CalcMP (PR, IM, Q, MP, IY, P, NM);

	NM = NY * 12;
	Interest = PR * IM;
	balance = PR;

	printinfo (PR, IY, IM, NY, NM, MP);

	printTable(Month, balance, MP, Interest, total, NM, IM);
	

system ("pause");
return 0;
}


void CalcMP (float PR, float IM, float Q, float MP, float IY, float P, int NM)
{

		IM = (IY / 12) / 100;
		P = pow((1 + IM), NM);
		Q = (P/ (P - 1));
		MP = (PR * IM * Q);

		return;
}
What's giving you trouble? Can you explain a bit more,please.
Last edited on
when i run it i can enter the loan, interest and years but when it gets to the calcMP function it gets an error saying NM is being used without being initialized. It does it for all the other variables in calcMP printinfo and printTable
Last edited on
Maybe I missed it but it appears as if you have not assigned a value to PR. Then you try to use it in your MP = ( PR * IM * Q ) , this will not work because PR has no value. Simple Fix = Give PR a value =]
Last edited on
No its there, line 25 you input the value
On 35, it looks like you are using IM without having given it a value.
IM is given a value in the calcMP function on line 32. all the equations and things work its just getting the variables to go into the function thats messing me up
No, it isn't. The parameter IM is given a value inside the function, but the variable IM in main() is left uninitialized.
Reagrding variable NM: I think you should move line 34, to line 31....it should be before the function call...

And regarding variable IM, it should be passed by address (if working in C) or reference (if working in C++)


CHANGES FOR C++:(by reference)
line 48 void CalcMP (float PR, float & IM, float Q, float MP, float IY, float P, int NM);


CHANGES FOR C: (by address)

line 32 CalcMP (PR, &IM, Q, MP, IY, P, NM);


void CalcMP (float PR, float *IM, float Q, float MP, float IY, float P, int NM)
{

*IM = (IY / 12) / 100;
P = pow((1 + *IM), NM);
Q = (P/ (P - 1));
MP = (PR * (*IM) * Q);

return;
}

If you need the calculated values of P, Q or MP (that are calculated in CalcMp function ) back in main for print functions, you should pas all these by address/reference
Last edited on
Topic archived. No new replies allowed.