Help with Homework, functions, precision, outputs

Here is my question...


Write a program which prompts the user to enter the principal, annual interest rate, the number of years and the number of payments per year. Then using this information, calculate the monthly payment for the loan.
Use the following functions; note two are void functions one is float returning:
Read_Loan_Info(Principal, Rate, Years, PaymentsPerYear) MonthlyPayment(Principal, Rate, Years, PaymentsPerYear)
Show_Table(Principal, Rate, Years, PaymentsPerYear)



I am trying to get the output to be this..



Principal            $11000.00
Interest Rate        10.00%
No. of Years         4
Payments per year    12
No. of Payments      48 
Monthly Payment      $278.99


However My output looks like this...

Enter the Principal --> 11000

Enter the Interst Rate --> 10

Enter the Years --> 4

Enter the Period --> 12


Principal           11000
Interest Rate       10
No. of Years        4
Payments per year   1606416624
No. of Payments     2130699200
Monthly Payment     9166.67
Would you like use again?  Y or N?


Here is my code. I am working on the set precision but why are my outputs so far off? I'm also not sure what the first function is suppose to be. my teacher has not replied yet so any ideas would help! thanks in advance!



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void Read_Loan_Info(float Principal, float Rate, int Years, int PaymentsPerYear);
float MonthlyPayment(float Principal, float Rate, int Years, int PaymentsPerYear);
void Show_Table(float Principal, float Rate, int Years, int PaymentsPerYear);


int main ()
{

    int Years, PaymentsPerYear;
        
    float Rate, Principal;
    
    char Again = 'Y';
    
    if (Again == 'Y' || Again == 'y')
    {
        
    cout << "Enter the Principal --> ";
        cin >> Principal;
    
    cout << "\nEnter the Interst Rate --> ";
        cin >> Rate;
    
    cout << "\nEnter the Years --> ";
        cin >> Years;
    
    cout << "\nEnter the Period --> ";
        cin >> PaymentsPerYear;
    
    Read_Loan_Info( Principal, Rate, Years, PaymentsPerYear);
    
    MonthlyPayment( Principal, Rate, Years, PaymentsPerYear);
    
    Show_Table( Principal, Rate, Years, PaymentsPerYear);
        
    cout << "\nWould you like use again?  Y or N?";
        cin >> Again;
        
    }


}
void Read_Loan_Info(float Principal, float Rate, int Years, int PaymentsPerYear)
{
    
    
}
float MonthlyPayment(float Principal, float Rate, int Years, int PaymentsPerYear)
{
    int Term;
    float Interest_Rate, MonthPayment;
    
    Interest_Rate = Rate / 12;
    Term = Years * PaymentsPerYear;
    
    MonthPayment = Principal * (Interest_Rate / (1 - pow(Interest_Rate + 1,-Term)));
    
    return MonthPayment;
}
void Show_Table(float Principal, float Rate, int Years, int PaymentsPerYear)
{
    int TotalPayments;
    
    TotalPayments = Years * PaymentsPerYear;
    
    cout << "\nPrincipal           " << Principal;
    cout << "\nInterest Rate       " << Rate;
    cout << "\nNo. of Years        " << Years;
    cout << "\nPayments per year   " << PaymentsPerYear;
    cout << "\nNo. of Payments     " << TotalPayments;
    cout << "\nMonthly Payment     " << MonthlyPayment( Principal, Rate, Years, PaymentsPerYear);
}
Last edited on
Why are the variables static? There is no need for that in this case...

Apart from that, I do not know where you are getting that error from (you never call MonthlyPay with 4 floats ever). I would recommend just cleaning and recompiling your file, to remove any problems that could have arisen.
why can i not call the function with more then 4 floats?
According to the code you've shown, you've declared the function to take 7 floats, so you must call it with 7 floats.

You apparently have not shown us all your code. The linker error is telling you that you're calling monthly_pay with 4 floats somewhere, but you have no function named monthly_pay that accepts 4 flaots.

Edit: I get no such linker error when compiling the code you posted.
BTW, line 56 will give you bogus output. InterestRate is never initialized.
Last edited on
I have changed my code a bit. please let me know why my outputs so far off! i am working on set precision.
here is my code for the last function.

1
2
3
4
5
6
7
8
9
10
11
12
13
void Show_Table(float Principal, float Rate, int Years, int PaymentsPerYear)
{
    int TotalPayments;
    
    TotalPayments = Years * PaymentsPerYear;
    
    cout << "\nPrincipal           " << "$" << setprecision(2) << Principal;
    cout << "\nInterest Rate       " << Rate << "%";
    cout << "\nNo. of Years        " << << setiosflags(fixed) << Years;
    cout << "\nPayments per year   " << PaymentsPerYear;
    cout << "\nNo. of Payments     " << TotalPayments;
    cout << "\nMonthly Payment     " << setprecision(2) << MonthlyPayment( Principal, Rate, Years, PaymentsPerYear);
}


I still need help on how i am getting these crazy numbers though. please help.
The user enters a value for Period which doesn't seem to get used?

I don't see that PaymentsPerYear ever gets a valid value, either through initialization or through user input. But you send it to the functions to calculate and then print out info.
I had not updated. i have period as payments per year. this is still not helping me with how i am getting these numbers however. thanks tho.

i am suppose to use these equations...

1
2
3
4
5
    
Interest_Rate = Rate / 12;
Term = Years * PaymentsPerYear;
    
MonthPayment = Principal * (Interest_Rate / (1 - pow(Interest_Rate + 1,-Term)));


but can anyone explain why i am dividing rate by 12? i do not get it...
how am i suppose to make 10 = .1 for this to work right...

I finished it on my own. thanks whoever helped.
Last edited on
Topic archived. No new replies allowed.