Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

12.) (25) Write one complete C++ program to calculate the future value of an Inv

ID: 668302 • Letter: 1

Question


12.) (25) Write one complete C++ program to calculate the future value of an Investment. Three values will be Inputted Into the program ? an Investment value (like 10,000.00), an interest rate (like 0.065) and the number of years. Your program should calculate the future value of the Investment given the following formula: future value = Investment * ( 1 + Interest rate)^year Print out the original Investment value, the Interest rate, the number of years, and the future value. You can assume that the Interest rate Is an annual value and Is compounded annually.

Explanation / Answer

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>

//using namespace std;

const float ANNUAL_INTEREST_RATE = 0.06;

float computeFutureValue(float accountBalance, int periodsPerYear, int years);

int main() {
ifstream accountsFile("lab04a-data.txt");
string line;

// make sure we have a good ifstream
if (!accountsFile) {
   cout << "File 'lab04a-data.txt' could not be opened." << endl;
   return 1;
}

// iterate through lines of file
while (getline(accountsFile, line)) {
   // use string stream to conveniently extract numbers:
   istringstream buffer(line);
   float accountBalance, futureValue;
   int periodsPerYear, years;

   // if the line can't be coerced to float, int, int, it's an invalid line
   // and we'll just stop here.
   if (!(buffer >> accountBalance >> periodsPerYear >> years)) { break; }

   // output floats to 2 decimal places, show point:
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

   // print future value:
   cout << "$" << accountBalance << ", "
           << (ANNUAL_INTEREST_RATE * 100) << "% rate, "
           << "compounded " << periodsPerYear << " times a year "
           << "for " << years << " years yields: $"
           << computeFutureValue(accountBalance, periodsPerYear, years)
           << endl;
}

return 0;
}

float computeFutureValue(float accountBalance, int periodsPerYear, int years) {
return accountBalance * (
   pow(1 + (ANNUAL_INTEREST_RATE / periodsPerYear), years * periodsPerYear));
}

lab04- data.txt

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote