Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. /*
  2. * lesson9part2.cpp
  3. *
  4. * Created on: Nov 17, 2019
  5. * Author: Braydon Medeiros
  6. *
  7. * This program calculates the future value in a bank account based on
  8. * the current amount, interest rate, and number of months it remains in
  9. * the back. Input and output is done through input and output files.
  10. */
  11.  
  12.  
  13. #include <math.h>
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <string>
  17. #include <fstream>
  18.  
  19. using namespace std;
  20.  
  21. double futureValue(double presentValue, double interestRate, int months);
  22. int read (double & presentValue, double & interestRate, int & months, ifstream & inputFile);
  23. void display (double presentValue, double interestRate, int months, double futureValue, ifstream & outputFile);
  24.  
  25. int main()
  26. {
  27. double presentValue, interestRate, valueFuture;
  28. int months, counter;
  29. string fileName;
  30.  
  31. cout << "Enter input file name" << endl;
  32.  
  33. cin >> fileName;
  34.  
  35. ifstream inputFile;
  36. inputFile.open(fileName);
  37.  
  38. if (!(inputFile.is_open()))
  39. {
  40. cout << "File \"" << fileName << "\" could not be opened" << endl;
  41. return 1;
  42. }
  43.  
  44. ofstream outputFile;
  45. outputFile.open("output.xls");
  46.  
  47. if (!(outputFile.is_open()))
  48. {
  49. cout << "File \"output.xls\" could not be opened" << endl;
  50. inputFile.close();
  51. return 1;
  52. }
  53.  
  54.  
  55. while(counter)
  56. {
  57. counter = read(presentValue, interestRate, months, inputFile);
  58. if (counter == 1)
  59. {
  60. valueFuture = futureValue(presentValue, interestRate, months);
  61. display (presentValue, interestRate, months, valueFuture, outputFile);
  62. }
  63. if (counter == 2)
  64. {
  65. cout << presentValue << " " << interestRate << " " << months << endl;
  66. cout << "One or more of the above values are not greater than zero" << endl;
  67. }
  68. }
  69.  
  70.  
  71.  
  72. inputFile.close();
  73. outputFile.close();
  74. return 0;
  75. }
  76.  
  77. double futureValue(double presentValue, double interestRate, int months)
  78. {
  79. double futureValue;
  80. futureValue = presentValue * pow((interestRate + 1), months);
  81. return futureValue;
  82. }
  83.  
  84. int read (double & presentValue, double & interestRate, int & months, ifstream & inputFile)
  85. {
  86. cin >> presentValue >> interestRate >> months;
  87. interestRate = interestRate / 100;
  88. if(presentValue < 0 || interestRate < 0 || months < 0)
  89. return 2;
  90. if(inputFile.eof())
  91. return 0;
  92. return 1;
  93. }
  94.  
  95. void display (double presentValue, double interestRate, int months, double futureValue, ifstream & outputFile)
  96. {
  97. static bool firstRun = 1;
  98. if(firstRun) //Outputs a header on first run of the function and disables it after use.
  99. {
  100. outputFile << "Future Value\tPresent Value\tMonthly Interest\tMonths" << endl;
  101. firstRun = 0;
  102. }
  103.  
  104. outputFile << presentValue << "/t" << interestRate << "/t" << months << "/t"<< futureValue << endl;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement