Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. #include <cmath>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     cout << "Welcome to the Financial Calculator!\n";
  10.     cout << endl;
  11.  
  12.     float loanAmount{};
  13.     float interestRate{};
  14.     float loanLength{};
  15.     float payAmount{};
  16.     float convertedRate{};
  17.     int menuSelection{};
  18.    
  19.     vector<float> financialComponents;
  20.    
  21.    
  22.  
  23.     do {
  24.         // Allow user to determine which calculator to enter.
  25.         cout << "(1) Monthly Payment Calculator\n(2) Loan Amount Calculator\n(3) Loan Length "
  26.             "Calculator\n(4) Exit\n";
  27.         cout << endl;
  28.         cout << "Please choose an appropriate menu option number: ";
  29.         cin >> menuSelection;
  30.         cout << endl;
  31.  
  32.         // Gather input and process outputs depending on which selection is made.
  33.         // Input validation.
  34.  
  35.         while (menuSelection < 1 || menuSelection > 4) {
  36.             cout << "Please enter a valid menu option!";
  37.             cin >> menuSelection;
  38.         }
  39.  
  40.         switch (menuSelection) {
  41.         case 1:
  42.             cout << "Please enter your loan amount: ";
  43.             cin >> loanAmount;
  44.             cout << "Please enter your interest rate, in decimal format: ";
  45.             cin >> interestRate;
  46.             cout << "Please enter your loan length (amount of monthly payments): ";
  47.             cin >> loanLength;
  48.             convertedRate = interestRate / 12;
  49.             payAmount = (loanAmount * convertedRate * pow((1 + convertedRate), loanLength)) /
  50.                 (pow((1 + convertedRate), loanLength) - 1);
  51.             cout << endl;
  52.             // Display output to user.
  53.             // Set precision to two for dollar amounts.
  54.  
  55.             cout << fixed << setprecision(2);
  56.             cout << "Your monthly payment will be \t$" << payAmount << endl;
  57.             cout << endl;
  58.  
  59.             financialComponents.push_back(loanAmount);
  60.             financialComponents.push_back(interestRate);
  61.             financialComponents.push_back(loanLength);
  62.             financialComponents.push_back(payAmount);
  63.  
  64.  
  65.             break;
  66.  
  67.         case 2:
  68.  
  69.             cout << "Please enter your payment amount: ";
  70.             cin >> payAmount;
  71.             cout << "Please enter your interest rate, in decimal format: ";
  72.             cin >> interestRate;
  73.             cout << "Please enter your loan length (amount of monthly payments): ";
  74.             cin >> loanLength;
  75.             convertedRate = interestRate / 12;
  76.             loanAmount = (payAmount / convertedRate) * (1 - (1 / pow((1 + convertedRate), loanLength)));
  77.             cout << endl;
  78.             // Display output to user.
  79.             // Set precision to two for dollar amounts.
  80.  
  81.             cout << fixed << setprecision(2);
  82.             cout << "Your principal loan amount is \t$" << loanAmount << endl;
  83.             cout << endl;
  84.  
  85.             financialComponents.push_back(loanAmount);
  86.             financialComponents.push_back(interestRate);
  87.             financialComponents.push_back(loanLength);
  88.             financialComponents.push_back(payAmount);
  89.  
  90.             break;
  91.  
  92.         case 3:
  93.  
  94.             cout << "Please enter your loan amount: ";
  95.             cin >> loanAmount;
  96.             cout << "Please enter your interest rate, in decimal format: ";
  97.             cin >> interestRate;
  98.             cout << "Please enter your payment amount: ";
  99.             cin >> payAmount;
  100.             convertedRate = interestRate / 12;
  101.             loanLength = log((payAmount / convertedRate) / ((payAmount / convertedRate) - loanAmount)) /
  102.                 log(1 + convertedRate);
  103.             cout << endl;
  104.             // Display output to user.
  105.             // Display output as a rounded number of payments.
  106.             cout << "You will have " << round(loanLength) << " payments." << endl;
  107.             cout << endl;
  108.  
  109.             financialComponents.push_back(loanAmount);
  110.             financialComponents.push_back(interestRate);
  111.             financialComponents.push_back(loanLength);
  112.             financialComponents.push_back(payAmount);
  113.  
  114.             break;
  115.  
  116.             // If 4 or an incorrect input for menu option is selected, kill program.
  117.         }
  118.         if (menuSelection == 4) {
  119.             cout << "Thank you for using the Financial Calculator!\n";
  120.             system("pause");
  121.             return (0);
  122.         }
  123.     } while (menuSelection != 4);
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement