# functions, sry i made mistake, dont know how to edit so just post new

Page 1 of 1

## 13 Replies - 1102 Views - Last Post: 03 May 2013 - 01:06 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=320211&amp;s=a48ae05b8c942c87d56b4dd88fad2ba2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 yank13

Reputation: 0
• Posts: 7
• Joined: 03-May 13

# functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 11:06 AM

caculate monthly payments for a loan using functions.

I'm sorry the code is not in order, I'm still in the troubleshooting stage. the program crashes. this is the assignment.

Write a c++ program to help a prospective borrower calculate the monthly payment for a loan. The program also prints the amortization(payoff)table to show the balance of the loan after monthly payment.

prompt user for input;

1)Amount of the loan:
2)Interet rate per year:
3)number of years:

formula;

NM=(NY *12)
IM =(IY/12)
P=(1+IM)NM
Q =(P/(P-1))
MP=(PR*IM*Q)

where
NY= Scheduled number of years to amortize the loan
NM=Scheduled number of months for the loan
IY=Interest rate per year(as a percentage)
IM=Interest rate/month (decimal)
PR=Principal (the amount of the loan)
P= The value of (1+IM)NM
Q= The value of P/(P-1)
MP= Monthly payment

main must call three functions: calculateMonthlyPayment, printInormation, printAmortizationTable.

i will make a attachments how output should look like. i already successfully run one without using functions, just dont get it with using functions, anyone help me to work it out? i can run it but those data just spread out
and it keeps running forever.

and sorry that i dont know how to attach my codes.

```#include <cstdlib>
#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

double calcMonthlyPayment (double prin, double IY, double NY);
void printInformation (double prin, double IM, double NM, double MP);
void printAmortizationTable (double PP, double MP, double NM, double IM, double IP, double OB, double NB);

int main()
{
double prin, IY, NY, MP;

cout<< " Loan Amount (Principal)? ";
cin>> prin;
cout<< endl;
cout<< " Interest rate per year (percent)? ";
cin>> IY;
cout<< endl;
cout<< " Number of years? ";
cin>> NY;
cout<< endl;
cout<< endl;

MP = calcMonthlyPayment(prin, IY, NY);

}

double calcMonthlyPayment (double prin, double IY, double NY)
{
double P, Q, IM, NM, MP;

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

printInformation (prin, IM, NM, MP);

return MP;
}

void printInformation (double prin, double IM, double NM, double MP)
{
double PP, IP, OB, NB;
cout<< " Loan Amout: " << prin << endl;
cout<< " Interest rate per month (IM): " << IM << endl;
cout<< " Number of months (NM): " << NM << endl;
cout<< " Monthly Payment is: " << MP << endl;
cout<< endl;
cout<< " Month Old Monthly Interest Principal New " << endl;
cout<< " Balance Payment Paid Paid Balance " << endl;

printAmortizationTable (PP, MP, NM, IM, IP, OB, NB);
}

void printAmortizationTable (double PP, double MP, double NM, double IM, double IP, double OB, double NB)
{
int i;
double prin;
OB = prin;
for(i=1; i <= NM; i++)
{
IP = OB * IM;
PP = MP - IP;
NB = OB - PP;
cout<< setw(2) << i << setw(14) << OB << setw(9) << MP << setw(13) << IP << setw(14) << PP << setw(15) << NB << endl;
OB = NB;
}

system("PAUSE");
}
```

thank you!

#### Attached image(s)

This post has been edited by jimblumberg: 03 May 2013 - 12:24 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.

Is This A Good Question/Topic? 0

## Replies To: functions, sry i made mistake, dont know how to edit so just post new

### #2 snoopy11

• Engineering ● Software

Reputation: 982
• Posts: 3,009
• Joined: 20-March 10

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 11:35 AM

Here is your code in code tags,

[ code] [ /code] (without spaces)

```#include <cstdlib>
#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

double calcMonthlyPayment (double prin, double IY, double NY);
void printInformation (double prin, double IM, double NM, double MP);
void printAmortizationTable (double PP, double MP, double NM, double IM, double IP, double OB, double NB);

int main()
{
double prin, IY, NY, MP;

cout<< " Loan Amount (Principal)? ";
cin>> prin;
cout<< endl;
cout<< " Interest rate per year (percent)? ";
cin>> IY;
cout<< endl;
cout<< " Number of years? ";
cin>> NY;
cout<< endl;
cout<< endl;

MP = calcMonthlyPayment(prin, IY, NY);

}

double calcMonthlyPayment (double prin, double IY, double NY)
{
double P, Q, IM, NM, MP;

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

printInformation (prin, IM, NM, MP);

return MP;
}

void printInformation (double prin, double IM, double NM, double MP)
{
double PP, IP, OB, NB;
cout<< " Loan Amout: " << prin << endl;
cout<< " Interest rate per month (IM): " << IM << endl;
cout<< " Number of months (NM): " << NM << endl;
cout<< " Monthly Payment is: " << MP << endl;
cout<< endl;
cout<< " Month Old Monthly Interest Principal New " << endl;
cout<< " Balance Payment Paid Paid Balance " << endl;

printAmortizationTable (PP, MP, NM, IM, IP, OB, NB);
}

void printAmortizationTable (double PP, double MP, double NM, double IM, double IP, double OB, double NB)
{
int i;
double prin;
OB = prin;
for(i=1; i <= NM; i++)
{
IP = OB * IM;
PP = MP - IP;
NB = OB - PP;
cout<< setw(2) << i << setw(14) << OB << setw(9) << MP << setw(13) << IP << setw(14) << PP << setw(15) << NB << endl;
OB = NB;
}

system("PAUSE");
}
```

First of all int main should return an integer usually zero for success.

second always initialise variables

like so

double PP=0.0, IP=0.0, OB=0.0, NB=0.0;

and finally can you clarify what it is you are asking...?

Thanks

Snoopy.

### #3 yank13

Reputation: 0
• Posts: 7
• Joined: 03-May 13

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:27 PM

y it just run like this forever?

### #4 jimblumberg

Reputation: 4647
• Posts: 14,571
• Joined: 25-December 09

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:29 PM

What values did you input into your program?

Try: 1000
12
1

Jim

This post has been edited by jimblumberg: 03 May 2013 - 12:31 PM

### #5 snoopy11

• Engineering ● Software

Reputation: 982
• Posts: 3,009
• Joined: 20-March 10

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:32 PM

yank13, on 03 May 2013 - 07:27 PM, said:

y it just run like this forever?

Because you are not initialising everyone of your variables.

uninitialised variables contain junk values so it can run a long time.

If you would just initialise every single double you have to 0.0 it would not do that.

Regards

Snoopy.

### #6 yank13

Reputation: 0
• Posts: 7
• Joined: 03-May 13

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:32 PM

input: prin, IY, NY. There are formula for others

### #7 snoopy11

• Engineering ● Software

Reputation: 982
• Posts: 3,009
• Joined: 20-March 10

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:35 PM

no Jim means

what values did you try when the program asked you for

amount of loan

interest

and

repayment period

### #8 yank13

Reputation: 0
• Posts: 7
• Joined: 03-May 13

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:36 PM

but how i gonna initialise those PP if i need to get that from a formula PP = MP - IP; and MP = (prin * IM * Q);, IP = OB * IM;

ohh, sorry, my bad, i input 5000, 11, 1. example from homework sheet

### #9 jimblumberg

Reputation: 4647
• Posts: 14,571
• Joined: 25-December 09

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:41 PM

This is the output I get from your program:

``` Loan Amount (Principal)? 5000

Interest rate per year (percent)? 11

Number of years? 1

Loan Amout:                   5000
Interest rate per month (IM): 0.00916667
Number of months (NM):        12
Monthly Payment is:           441.908

Month     Old     Monthly     Interest     Principal         New
Balance   Payment       Paid         Paid          Balance
1  -1.37481e-41  441.908 -1.26024e-43       441.908       -441.908
2      -441.908  441.908     -4.05083       445.959       -887.867
3      -887.867  441.908     -8.13878       450.047       -1337.91
4      -1337.91  441.908     -12.2642       454.173       -1792.09
5      -1792.09  441.908     -16.4275       458.336       -2250.42
6      -2250.42  441.908     -20.6289       462.537       -2712.96
7      -2712.96  441.908     -24.8688       466.777       -3179.74
8      -3179.74  441.908     -29.1476       471.056       -3650.79
9      -3650.79  441.908     -33.4656       475.374       -4126.17
10      -4126.17  441.908     -37.8232       479.731        -4605.9
11       -4605.9  441.908     -42.2207       484.129       -5090.03
12      -5090.03  441.908     -46.6586       488.567       -5578.59

```

These are the warnings I got when I compiled your code:

Quote

main.cpp||In function ‘int main()’:|
main.cpp|15|warning: variable ‘MP’ set but not used [-Wunused-but-set-variable]|
main.cpp||In function ‘void printAmortizationTable(double, double, double, double, double, double, double)’:|
main.cpp|66|warning: ‘prin’ is used uninitialized in this function [-Wuninitialized]|
main.cpp||In function ‘void printInformation(double, double, double, double)’:|
main.cpp|59|warning: ‘PP’ is used uninitialized in this function [-Wuninitialized]|
main.cpp|59|warning: ‘IP’ is used uninitialized in this function [-Wuninitialized]|
main.cpp|59|warning: ‘OB’ is used uninitialized in this function [-Wuninitialized]|
main.cpp|59|warning: ‘NB’ is used uninitialized in this function [-Wuninitialized]|
||=== Build finished: 0 errors, 6 warnings ===|

Jim

### #10 snoopy11

• Engineering ● Software

Reputation: 982
• Posts: 3,009
• Joined: 20-March 10

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:44 PM

for the input 5000, 11, and 1 I get with your program

Regards

Snoopy.

### #11 yank13

Reputation: 0
• Posts: 7
• Joined: 03-May 13

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:49 PM

um, why you can stop there? i cant even pause, it would just keep running, because i didnt initialise? but i need to give the value to them by the formula

### #12 snoopy11

• Engineering ● Software

Reputation: 982
• Posts: 3,009
• Joined: 20-March 10

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:55 PM

You need to basically

initialise here

```void printInformation (double prin, double IM, double NM, double MP)
{
double PP=0.0, IP=0.0, OB=0.0, NB=0.0;

```

and here

```void printAmortizationTable (double PP, double MP, double NM, double IM, double IP, double OB, double NB)
{
int i;
double prin=0.0;
```

what compiler/IDE are you using because your warning levels are set too low

Best Wishes

Snoopy.

### #13 jimblumberg

Reputation: 4647
• Posts: 14,571
• Joined: 25-December 09

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 01:06 PM

Actually there doesn't even seem to be a reason to pass most of those variables into your function, they don't have a value in the calling function and since they are passed by value there is no need to pass them.

I would define printAmortizationTable more like:

```void printAmortizationTable (double MP, double NM, double IM, double prin)
{
int i;
double OB, IP, PP, NB;

```

Of course you'll need to change the function call and prototype to match.

You should study the function tutorials contained in my signature.

Jim

### #14 yank13

Reputation: 0
• Posts: 7
• Joined: 03-May 13

## Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 01:06 PM

Using Dev-C++ 4.9.9.2
I am newbie I dont knwo much about that sorry,um, and i initialised all the places u said, but i got the error

C:\Users\KAIYAN~1\AppData\Local\Temp\ccWUjaaa.o(.text+0x28c) In function `Z18calcMonthlyPaymentddd':
[Linker error] undefined reference to `printInformation(double, double, double, double)'
C:\Users\KAIYAN~1\AppData\Local\Temp\ccWUjaaa.o(.text+0x28c) ld returned 1 exit status

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }