Cleaning up and simplifying my code.
Oct 19, 2018 at 9:52pm UTC
Hi. This is definitely homework and I feel like I've got most of it down. The code works, but I feel like there's an easier way to do the last bit. I'm not supposed to use global variables.
Is there a way to clean up the last portion? Why can I only get the math to work if I plug in the monPayment calculation after each noMonth declaration?
I'm very new to all of this. I really appreciate any tips or hints.
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
#include "pch.h"
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
float price; // Price of vehicle.
float tradeIn; // Trade in price for vehicle.
float downPayment; // Down payment.
float loanAmt; // Loan amount (calculated).
float annualIntRate; // Annual interest rate (fraction).
float annualIntPercent; // Annual interest rate (percent).
float monIntRate; // Monthly interest rate (fraction).
int noMonths; // Number of monthly payments (12, 24, 36, 48 & 60).
float monPayment; // Monthly payment.
while (true ) // While loop for price of vehicle.
{
cout << "Enter the price of the vehicle: " ;
cin >> price;
cout << "You entered $" << fixed << setprecision(2) << price << "." << endl; // Set precision at 2.
if (price < 50 || price > 50000) // Validating price.
{
cout << "Invalid price. Must be between $50-$50,000." << endl;
continue ;
}
else
{
break ; // End of loop.
}
}
while (true ) // While loop for trade in value.
{
cout << "Enter the trade in price of your vehicle: " ;
cin >> tradeIn;
cout << "You entered $" << tradeIn << "." << endl;
if (tradeIn <= 0 || tradeIn > price) // Validating trade it price.
{
cout << "Invalid trade in price. Must be greater than $0 and less than $"
<< price << "." << endl;
continue ;
}
else
{
break ; // End of loop.
}
}
while (true ) // While loop for down payment.
{
cout << "Enter the down payment for the vehicle: " ;
cin >> downPayment;
cout << "You entered $" << downPayment << "." << endl;
if (downPayment < 0 || downPayment > price - tradeIn) // Validating down payment.
{
cout << "Invalid down payment. Your down payment should be less than $"
<< price - tradeIn << "." << endl;
continue ;
}
else
{
break ; // End of loop.
}
}
while (true ) // While loop for annual interest rate.
{
cout << "Enter the annual interest rate of the vehicle as a decimal: " ;
cin >> annualIntRate;
annualIntPercent = annualIntRate * 100.0; // Calculate annual interest rate as a percent.
cout << "You entered " << annualIntPercent << "%." << endl; // Convert to %.
if (annualIntRate < 0 || annualIntRate > .50) // Validating annual interest rate.
{
cout << "Invalid annual interest rate. Your rate should be between 0 and .50." << endl;
continue ;
}
else
{
break ; // End of loop.
}
}
monIntRate = annualIntRate / 12.0; // Calculate monthly interest rate.
loanAmt = price - downPayment - tradeIn; // Calculate loan amount.
cout << "\n Some Guy's Used Cars:" << "\n" << endl; // Title for table.
cout << setw(25) << "Vehicle Price" << setw(15) << "$" << price << endl; // Price.
cout << setw(25) << "Trade In Value" << setw(15) << "$" << tradeIn << endl; // Trade in value.
cout << setw(25) << "Down Payment" << setw(15) << "$" << downPayment << endl; // Down payment.
cout << setw(48) << "----------" << endl;
cout << setw(25) << "Loan Amount" << setw(15) << "$" << loanAmt << "\n" << endl; // Loan amount.
cout << setw(25) << "Annual interest rate" << setw(20) << annualIntPercent << "% \n" << endl; // Annual interest rate.
cout << "Monthly Payment Options: \n" << endl; // Title for table.
noMonths = 12;
monPayment = (loanAmt * monIntRate) / (1.0 - pow(1 + monIntRate, -noMonths)); // Payment for 12 months.
cout << setw(25) << "12 Months" << setw(15) << "$" << monPayment << endl;
noMonths = 24;
monPayment = (loanAmt * monIntRate) / (1.0 - pow(1 + monIntRate, -noMonths)); // Payment for 24 months.
cout << setw(25) << "24 Months" << setw(15) << "$" << monPayment << endl;
noMonths = 36;
monPayment = (loanAmt * monIntRate) / (1.0 - pow(1 + monIntRate, -noMonths)); // Payment for 36 months.
cout << setw(25) << "36 Months" << setw(15) << "$" << monPayment << endl;
noMonths = 48;
monPayment = (loanAmt * monIntRate) / (1.0 - pow(1 + monIntRate, -noMonths)); // Payment for 48 months.
cout << setw(25) << "48 Months" << setw(15) << "$" << monPayment << endl;
noMonths = 60;
monPayment = (loanAmt * monIntRate) / (1.0 - pow(1 + monIntRate, -noMonths)); // Payment for 60 months.
cout << setw(25) << "60 Months" << setw(15) << "$" << monPayment << endl;
return 0;
}
Last edited on Oct 20, 2018 at 6:05am UTC
Oct 19, 2018 at 10:06pm UTC
1 2 3 4 5
cout << "Monthly Payment Options: \n\n"
for (int noMonths = 12; noMonths <= 60; noMonths += 12) {
double monPayment = (loanAmt * monIntRate) / (1.0 - pow(1 + monIntRate, -noMonths));
cout << setw(18) << noMonths << " Months" << setw(15) << "$" << monPayment << '\n' ;
}
I don't know if I got the spacing right.
You can simplify your input loops a little, too.
1 2 3 4 5 6 7 8 9 10 11
// you just need to do this once.
cout << fixed << setprecision(2);
while (true ) {
cout << "Enter the price of the vehicle: " ;
cin >> price;
cout << "You entered $" << price << ".\n" ; // <-- this is kind of pointless (they can already see what they just entered)
if (price >= 50 && price <= 50000)
break ;
cout << "Invalid price. Must be between $50-$50,000.\n" ;
}
Last edited on Oct 19, 2018 at 10:11pm UTC
Oct 19, 2018 at 10:19pm UTC
Thank you, this was incredibly helpful. I knew I'd learned the concept but I couldn't figure out how to implement it.
Topic archived. No new replies allowed.