Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * lesson9part2.cpp
- *
- * Created on: Nov 17, 2019
- * Author: Braydon Medeiros
- *
- * This program calculates the future value in a bank account based on
- * the current amount, interest rate, and number of months it remains in
- * the back. Input and output is done through input and output files.
- */
- #include <math.h>
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <fstream>
- using namespace std;
- double futureValue(double presentValue, double interestRate, int months);
- int read (double & presentValue, double & interestRate, int & months, ifstream & inputFile);
- void display (double presentValue, double interestRate, int months, double futureValue, ifstream & outputFile);
- int main()
- {
- double presentValue, interestRate, valueFuture;
- int months, counter;
- string fileName;
- cout << "Enter input file name" << endl;
- cin >> fileName;
- ifstream inputFile;
- inputFile.open(fileName);
- if (!(inputFile.is_open()))
- {
- cout << "File \"" << fileName << "\" could not be opened" << endl;
- return 1;
- }
- ofstream outputFile;
- outputFile.open("output.xls");
- if (!(outputFile.is_open()))
- {
- cout << "File \"output.xls\" could not be opened" << endl;
- inputFile.close();
- return 1;
- }
- while(counter)
- {
- counter = read(presentValue, interestRate, months, inputFile);
- if (counter == 1)
- {
- valueFuture = futureValue(presentValue, interestRate, months);
- display (presentValue, interestRate, months, valueFuture, outputFile);
- }
- if (counter == 2)
- {
- cout << presentValue << " " << interestRate << " " << months << endl;
- cout << "One or more of the above values are not greater than zero" << endl;
- }
- }
- inputFile.close();
- outputFile.close();
- return 0;
- }
- double futureValue(double presentValue, double interestRate, int months)
- {
- double futureValue;
- futureValue = presentValue * pow((interestRate + 1), months);
- return futureValue;
- }
- int read (double & presentValue, double & interestRate, int & months, ifstream & inputFile)
- {
- cin >> presentValue >> interestRate >> months;
- interestRate = interestRate / 100;
- if(presentValue < 0 || interestRate < 0 || months < 0)
- return 2;
- if(inputFile.eof())
- return 0;
- return 1;
- }
- void display (double presentValue, double interestRate, int months, double futureValue, ifstream & outputFile)
- {
- static bool firstRun = 1;
- if(firstRun) //Outputs a header on first run of the function and disables it after use.
- {
- outputFile << "Future Value\tPresent Value\tMonthly Interest\tMonths" << endl;
- firstRun = 0;
- }
- outputFile << presentValue << "/t" << interestRate << "/t" << months << "/t"<< futureValue << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement