Editing car loan calculator code

I am a beginner c++ student with an assignment to create a car loan calculator. I am having trouble with the calculations of the monthly payment, which I assume is because the operands are of different types in the equation? I am also not able to produce the results for the number of payments, total amount owed and total interest paid. If someone could look over my code, I would appreciate any input to help get my program working correctly!

//Date: 2/15/2011
// Programmer: Lindsay Lewis
// Description: This program computes the monthly payment for a car loan

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{

// This step requires the user to input the type of vehicle
string car_type;
cout << "Enter the type of vehicle: ";
getline (cin,car_type);

// This step requires the user to input the purchase price of the car
double purchase_price;
cout << "Enter the purchase price of the car: ";
cin >> purchase_price;

// This step requires the user to input the amount of the down payment
double down_payment;
cout << "Enter the amount of your down payment: ";
cin >> down_payment;

// This step requires the user to input the number of years to pay off the car
int years;
cout << "Enter the number of years to pay off the car: ";
cin >> years;

// This step requires the user to input the interest rate
float interest_rate;
cout << "Enter the annual interest rate:";
cin >> interest_rate;

// This step calcuates the interest rate
double interest;
interest = interest_rate / 100;


// This step defines the value for the amount to be financed
double amount_financed;
amount_financed = purchase_price - down_payment ;

// This step defines the number of payments per year
int payments_year;
payments_year = 12;

// This step calculates the monthly payment
double monthly_payment;
monthly_payment = (amount_financed * (interest / payments_year)) /(1 - pow(1+\
interest / payments_year),(-payments_year * years));

// This step calculates the number of payments
double number_payments;
number_payments = years - monthly_payment;

// This step calculates the total amount owed
double total_amount_owed;
total_amount_owed = number_payments * monthly_payment;

// This step calculates the total interest paid
double total_interest_paid;
total_interest_paid = monthly_payment * number_payments - amount_financed;

// This step calculates the number of payments
double number_payments;
number_payments = years - monthly_payment;

// This step calculates the total amount owed
double total_amount_owed;
total_amount_owed = number_payments * monthly_payment;

// This step calculates the total interest paid
double total_interest_paid;
total_interest_paid = monthly_payment * number_payments - amount_financed;

cout << left;
cout << setw(40) << "Monthly Car Loan Payment Calculation" << endl;
cout << setw(40) << "========================================" << endl;
cout << setw(40) << "Type of Vehicle:" << car_type << endl;
cout << setw(40) << "Purchase Price:" << purchase_price << endl;
cout << setw(40) << "Down Payment:" << down_payment << endl;
cout << setw(40) << "Amount Financed:" << amount_financed << endl;
cout << setw(40) << "Length of Loan:" << years << endl;
cout << setw(40) << "Annual Interest Rate:" << interest_rate << endl;
cout << setw(40) << "Monthly Payment:" << monthly_payment << endl;
cout << setw(40) << "Number of Payments:" << number_payments << endl;
cout << setw(40) << "Total Amount Owed:" << total_amount_owed << endl;
cout << setw(40) << "Total Interest Paid:" << total_interest_paid << endl;

}

To debug this yourself, you could just print out the values of the variables as you go along and verify that they are correct. Then you can narrow it down to a line where a calculation goes wrong.

Most likely, there's integer division in there somewhere and some floating point calculation isn't returning what you'd expect. Just a guess, though.
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
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{

// This step requires the user to input the type of vehicle
string car_type;
cout << "Enter the type of vehicle: ";
getline (cin,car_type);

// This step requires the user to input the purchase price of the car
double purchase_price;
cout << "Enter the purchase price of the car: ";
cin >> purchase_price;

// This step requires the user to input the amount of the down payment
double down_payment;
cout << "Enter the amount of your down payment: ";
cin >> down_payment;

// This step requires the user to input the number of years to pay off the car
int years;
cout << "Enter the number of years to pay off the car: ";
cin >> years;

// This step requires the user to input the interest rate
float interest_rate;
cout << "Enter the annual interest rate:";
cin >> interest_rate;

// This step calcuates the interest rate
double interest;
interest = interest_rate / 100;


// This step defines the value for the amount to be financed
double amount_financed;
amount_financed = purchase_price - down_payment ;

// This step defines the number of payments per year
int payments_year;
payments_year = 12;

// This step calculates the monthly payment
double monthly_payment;
monthly_payment = (amount_financed * (interest / payments_year)) /(1 - pow(1+\
interest / payments_year),(-payments_year * years));

// This step calculates the number of payments
double number_payments;
number_payments = years - monthly_payment;

// This step calculates the total amount owed
double total_amount_owed;
total_amount_owed = number_payments * monthly_payment;

// This step calculates the total interest paid
double total_interest_paid;
total_interest_paid = monthly_payment * number_payments - amount_financed;

// This step calculates the number of payments
double number_payments;
number_payments = years - monthly_payment;

// This step calculates the total amount owed
double total_amount_owed;
total_amount_owed = number_payments * monthly_payment;

// This step calculates the total interest paid
double total_interest_paid;
total_interest_paid = monthly_payment * number_payments - amount_financed;

cout << left;
cout << setw(40) << "Monthly Car Loan Payment Calculation" << endl;
cout << setw(40) << "========================================" << endl;
cout << setw(40) << "Type of Vehicle:" << car_type << endl;
cout << setw(40) << "Purchase Price:" << purchase_price << endl;
cout << setw(40) << "Down Payment:" << down_payment << endl;
cout << setw(40) << "Amount Financed:" << amount_financed << endl;
cout << setw(40) << "Length of Loan:" << years << endl;
cout << setw(40) << "Annual Interest Rate:" << interest_rate << endl;
cout << setw(40) << "Monthly Payment:" << monthly_payment << endl;
cout << setw(40) << "Number of Payments:" << number_payments << endl;
cout << setw(40) << "Total Amount Owed:" << total_amount_owed << endl;
cout << setw(40) << "Total Interest Paid:" << total_interest_paid << endl;

}


use code tags.

The answers to your questions are mostly mathematical. Like to find number of payments do this:
1
2
3
4
5
6
int numberofpayments=0;
while(amountowed>0)
{
        amountowed-=payment;
        numberofpayments++;
}


and if you are really confused about types such as double and int interacting just play around with them making programs to see what happens when you plug them in.
Are they teaching you functions in your programming class yet? This program would benefit a lot from the use of functions; would make for much prettier code at the very least.

edit: and by the way, put before and after your code when posting.
Last edited on
We have not yet covered code tags or functions in the programming class yet. I'm sorry to say that I do not have any clue as to how to use code tags. Thanks for the input though!
in this forum when you post there is a format box to the right. make sure your cursor is on a new line and then click the <> button you will get a [] with the word code in it and a closing tag with the word /code in it. Put the code you want us to look at in between the two tags.

that will make your code appear like this:
 
//your code goes here. 


Also note the preview button, so that you can play with it before posting.
Topic archived. No new replies allowed.