Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <iomanip>
- #include <iostream>
- #include <vector>
- using namespace std;
- int main() {
- cout << "Welcome to the Financial Calculator!\n";
- cout << endl;
- float loanAmount{};
- float interestRate{};
- float loanLength{};
- float payAmount{};
- float convertedRate{};
- int menuSelection{};
- vector<float> financialComponents;
- do {
- // Allow user to determine which calculator to enter.
- cout << "(1) Monthly Payment Calculator\n(2) Loan Amount Calculator\n(3) Loan Length "
- "Calculator\n(4) Exit\n";
- cout << endl;
- cout << "Please choose an appropriate menu option number: ";
- cin >> menuSelection;
- cout << endl;
- // Gather input and process outputs depending on which selection is made.
- // Input validation.
- while (menuSelection < 1 || menuSelection > 4) {
- cout << "Please enter a valid menu option!";
- cin >> menuSelection;
- }
- switch (menuSelection) {
- case 1:
- cout << "Please enter your loan amount: ";
- cin >> loanAmount;
- cout << "Please enter your interest rate, in decimal format: ";
- cin >> interestRate;
- cout << "Please enter your loan length (amount of monthly payments): ";
- cin >> loanLength;
- convertedRate = interestRate / 12;
- payAmount = (loanAmount * convertedRate * pow((1 + convertedRate), loanLength)) /
- (pow((1 + convertedRate), loanLength) - 1);
- cout << endl;
- // Display output to user.
- // Set precision to two for dollar amounts.
- cout << fixed << setprecision(2);
- cout << "Your monthly payment will be \t$" << payAmount << endl;
- cout << endl;
- financialComponents.push_back(loanAmount);
- financialComponents.push_back(interestRate);
- financialComponents.push_back(loanLength);
- financialComponents.push_back(payAmount);
- break;
- case 2:
- cout << "Please enter your payment amount: ";
- cin >> payAmount;
- cout << "Please enter your interest rate, in decimal format: ";
- cin >> interestRate;
- cout << "Please enter your loan length (amount of monthly payments): ";
- cin >> loanLength;
- convertedRate = interestRate / 12;
- loanAmount = (payAmount / convertedRate) * (1 - (1 / pow((1 + convertedRate), loanLength)));
- cout << endl;
- // Display output to user.
- // Set precision to two for dollar amounts.
- cout << fixed << setprecision(2);
- cout << "Your principal loan amount is \t$" << loanAmount << endl;
- cout << endl;
- financialComponents.push_back(loanAmount);
- financialComponents.push_back(interestRate);
- financialComponents.push_back(loanLength);
- financialComponents.push_back(payAmount);
- break;
- case 3:
- cout << "Please enter your loan amount: ";
- cin >> loanAmount;
- cout << "Please enter your interest rate, in decimal format: ";
- cin >> interestRate;
- cout << "Please enter your payment amount: ";
- cin >> payAmount;
- convertedRate = interestRate / 12;
- loanLength = log((payAmount / convertedRate) / ((payAmount / convertedRate) - loanAmount)) /
- log(1 + convertedRate);
- cout << endl;
- // Display output to user.
- // Display output as a rounded number of payments.
- cout << "You will have " << round(loanLength) << " payments." << endl;
- cout << endl;
- financialComponents.push_back(loanAmount);
- financialComponents.push_back(interestRate);
- financialComponents.push_back(loanLength);
- financialComponents.push_back(payAmount);
- break;
- // If 4 or an incorrect input for menu option is selected, kill program.
- }
- if (menuSelection == 4) {
- cout << "Thank you for using the Financial Calculator!\n";
- system("pause");
- return (0);
- }
- } while (menuSelection != 4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement