Illuminatiiiiii
12/19/2019 - 1:11 AM

C++ Practice: Compound Interest Program

#include <iostream>

using namespace std;

//Ask user for the values so we can then make the calculation
void promptValues(double &balance, double &interestRate, int &time, int &amount, int choice){

    if(choice == 1){
        cout << "Enter the starting balance: ";
        cin >> balance;

        cout << "Enter the interest rate as a decimal: ";
        cin >> interestRate;

        cout << "Enter the period of interest(years): ";
        cin >> time;

        cout << "Enter the compound rate: ";
        cin >> amount;
    }else{
        cout << "Enter the final balance: ";
        cin >> balance;

        cout << "Enter the interest rate as a decimal: ";
        cin >> interestRate;

        cout << "Enter the period of interest(years): ";
        cin >> time;

        cout << "Enter the compound rate: ";
        cin >> amount;
    }

}

double calculateFinalAmount(double startingBalance, double interestRate, int time, int amount){

    return (startingBalance * pow((1 + (interestRate / amount)), (time * amount)));

}

double calculatePrincipalAmount(double finalBalance, double interestRate, int time, int amount){

    return (finalBalance / pow((1 + (interestRate / amount)), (time * amount)));

}

int main() {

    //Display menu to user
    int choice;
    cout << "Welcome";
    do{
        cout << "Choose an option below: " << endl;
        cout << "-------------------------------" << endl;
        cout << "[1] - Find Compound Interest" << endl;
        cout << "[2] - Find Principal Amount" << endl;
        cout << "[0] EXIT Program" << endl;
        cout << "-------------------------------" << endl;
        cin >> choice;
        if(choice == 1){

            double startingBalance;
            double interestRate;
            int time;
            int amount;

            promptValues(startingBalance, interestRate, time, amount, 1);
            double final = calculateFinalAmount(startingBalance, interestRate, time, amount);

            cout << endl;
            cout << "----------------" << endl;
            cout << "Results: " << endl;
            cout << "Final Account Balance: $" << final << endl;

        }else if(choice == 2){

            double finalBalance;
            double interestRate;
            int time;
            int amount;

            promptValues(finalBalance, interestRate, time, amount, 2);
            double principal = calculatePrincipalAmount(finalBalance, interestRate, time, amount);

            cout << endl;
            cout << "----------------" << endl;
            cout << "Results: " << endl;
            cout << "Principal Value: $" << principal << endl;

        }else if(choice != 0){
            cout << "Invalid choice. Choose Again";
        }
    }while(choice != 0);

    cout << "Goodbye" << endl;

    return 0;
}