EvilZone
Programming and Scripting => C - C++ => Topic started by: Killeramor on October 15, 2014, 04:51:57 PM
-
Car Payment Calculator with Values in code.//Monthly Payment on New car
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num1;
int num2;
double num3;
int num4;
int num5;
double num6;
double num7;
float math;
math = pow(1+0.00541666666/12,-60);
float math2;
math2 = pow(0.00541666666,60);
//Down Payment
num1 = 2200;
//Car Price
num2 = 22000;
//Monthly interest rate
num3 = 0.00541666666;
//Number of Months all together
num4 = 60;
// Year
num5 = 12;
// APR
num6 = 6.5;
// First part of the Equation
num7 = 107.25;
cout << "Prices on my new car!" << endl;
cout << "Purchase Price= "<< num2 << endl;
cout << "Yearly APR= %"<< num6 << endl;
cout << "Monthly Interest= "<< num6 / num5 << endl;
cout << "Interest* (purchase-Downpayment)= "<< num3 * (num2 - num1)<< endl;
/* cout<< "Monthly carpayment= " << num3 * (num2 - num1) * ( math/ math -1); */
cout << "Monthly Payment= "<< (num2)*(num3/12)/(1-math) << endl;
system("PAUSE");
return 0;
}
Car Payment Calculator with Values being Input.
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
string name;
float price, dpmt, apr, pmt, years, months;
cout << "Hello!\n";
cout << "What is your name?";
cin >> name;
cout << "Hello, " << name << ", let me calculate your payments.\n\n" ;
cout << "What is the sales price? ";
cin >> price;
cout << endl;
cout << "What is the down payment? ";
cin >> dpmt;
cout << endl;
cout << "What is the APR? ";
cin >> apr;
cout << endl;
cout << "How many years will you finance for? ";
cin >> years;
cout << endl;
apr = apr / 100 / 12;
months = years * 12;
pmt = (apr * (price - dpmt)) * ((pow((1 + apr), months))
/(pow((1+ apr), months)-1));
cout << "Your monthly payment will be $" << pmt << endl << endl;
cout <<"Have a good day, "<< name <<" , thanks for putting me to work!" <<endl;
cout << endl;
system("PAUSE");
return 0;
}
-
int main()
{
// Variable names should be named after what they hold. num1, for instance, should be called downPayment, assuming your comments are correct.
// Oh, and separate declaration and assignment is pointless. Declare and assign is allowed: int leet = 1337;
int num1;
int num2;
double num3;
int num4;
int num5;
double num6;
double num7;
float math;
math = pow(1+0.00541666666/12,-60);
float math2;
math2 = pow(0.00541666666,60);
//Down Payment
num1 = 2200;
//Car Price
num2 = 22000;
//Monthly interest rate
num3 = 0.00541666666;
//Number of Months all together
num4 = 60;
// Year
num5 = 12;
// APR
num6 = 6.5;
// First part of the Equation
num7 = 107.25;
// It's good practice to document things that aren't obvious to a programmer. What if you need to change this in 25 years and have no idea how these work?
cout << "Prices on my new car!" << endl;
cout << "Purchase Price= "<< num2 << endl;
cout << "Yearly APR= %"<< num6 << endl;
cout << "Monthly Interest= "<< num6 / num5 << endl;
cout << "Interest* (purchase-Downpayment)= "<< num3 * (num2 - num1)<< endl;
/* cout<< "Monthly carpayment= " << num3 * (num2 - num1) * ( math/ math -1); */
cout << "Monthly Payment= "<< (num2)*(num3/12)/(1-math) << endl;
system("PAUSE"); // Bad idea. Doesn't work on all platforms and the only real case where this isn't a pain is if the process in launched from the Windows GUI, where terminal processes exit after completion.
return 0;
}
-
Refrain from using namespace std. This is bad practice.
See here for reasons: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
-
Refrain from using namespace std. This is bad practice.
See here for reasons: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice (http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)
Taken and noted. I see why it would cause errors.
int main()
{
// Variable names should be named after what they hold. num1, for instance, should be called downPayment, assuming your comments are correct.
// Oh, and separate declaration and assignment is pointless. Declare and assign is allowed: int leet = 1337;
int num1;
int num2;
double num3;
int num4;
int num5;
double num6;
double num7;
float math;
math = pow(1+0.00541666666/12,-60);
float math2;
math2 = pow(0.00541666666,60);
//Down Payment
num1 = 2200;
//Car Price
num2 = 22000;
//Monthly interest rate
num3 = 0.00541666666;
//Number of Months all together
num4 = 60;
// Year
num5 = 12;
// APR
num6 = 6.5;
// First part of the Equation
num7 = 107.25;
// It's good practice to document things that aren't obvious to a programmer. What if you need to change this in 25 years and have no idea how these work?
cout << "Prices on my new car!" << endl;
cout << "Purchase Price= "<< num2 << endl;
cout << "Yearly APR= %"<< num6 << endl;
cout << "Monthly Interest= "<< num6 / num5 << endl;
cout << "Interest* (purchase-Downpayment)= "<< num3 * (num2 - num1)<< endl;
/* cout<< "Monthly carpayment= " << num3 * (num2 - num1) * ( math/ math -1); */
cout << "Monthly Payment= "<< (num2)*(num3/12)/(1-math) << endl;
system("PAUSE"); // Bad idea. Doesn't work on all platforms and the only real case where this isn't a pain is if the process in launched from the Windows GUI, where terminal processes exit after completion.
return 0;
}
Noted. Professor never really commented on our code, so I developed bad habits in some areas. Specially in commenting, because I never really comment anything. Even though he told us to always comment.
-
When you start coding some complex stuff you will develop the commenting habit after afew headaches; the part where you totally don't understand your own code.
That code could use a class, not recommended but i think you could use some practice with something as simple as this.
-
First code:
1. These variables are given values but never used: num4, num, math2
Second code:
1. You use std::string but don't include the <string> header
2. std::cin can fail for invalid input based on the type for assignment, which will make the rest of the insertions with the >> fail, in this case at least, since they are all of the same type (float). The proper thing to do would be to correct the stream if it leaves the good state (since you're using std::cin especially), or instantiate a disposable input stream and construct it with the buffer contents of std::cin, allowing it to possibly fail as we don't care
3. For string input, what if the person wanted to input their first and last name? It'll cause the rest of the extractions to fail because by default input is delimited by whitespace, which is why I'd suggest using getline() instead
For both:
1. Declaring all the variables at the top in one place is bad practice IMO
2. <cmath> is the C++ version of the include for <math.h>
3. Look into naming conventions