This forum is in archive mode. You will not be able to post new content.

Author Topic: Basic Monthly Car Payment Calculator. C++  (Read 1267 times)

0 Members and 2 Guests are viewing this topic.

Offline Killeramor

  • Peasant
  • *
  • Posts: 115
  • Cookies: 5
  • Programming tutor for Qbasic, and beginner C++.
    • View Profile
Basic Monthly Car Payment Calculator. C++
« on: October 15, 2014, 04:51:57 PM »



Car Payment Calculator with Values in code.
Code: [Select]
//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.

Code: [Select]
#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;
}
Knowledge is free. Share what you know, help others grow. We all start somewhere.

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: Basic Monthly Car Payment Calculator. C++
« Reply #1 on: October 15, 2014, 11:59:55 PM »
Code: [Select]
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;
}

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Basic Monthly Car Payment Calculator. C++
« Reply #2 on: October 16, 2014, 10:46:54 AM »
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

Offline Killeramor

  • Peasant
  • *
  • Posts: 115
  • Cookies: 5
  • Programming tutor for Qbasic, and beginner C++.
    • View Profile
Re: Basic Monthly Car Payment Calculator. C++
« Reply #3 on: October 16, 2014, 03:07:55 PM »
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


Taken and noted. I see why it would cause errors.


Code: [Select]
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.
Knowledge is free. Share what you know, help others grow. We all start somewhere.

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: Basic Monthly Car Payment Calculator. C++
« Reply #4 on: October 18, 2014, 07:50:47 PM »
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.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Basic Monthly Car Payment Calculator. C++
« Reply #5 on: October 19, 2014, 12:18:26 AM »
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
« Last Edit: October 19, 2014, 12:19:04 AM by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.