Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. Code to copy
  2.  
  3. //Include the needed header files
  4. #include <fstream>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <string>
  8. #include <cmath>
  9. using namespace std;
  10.  
  11. //Function declarations
  12. unsigned int readDataFromFile(ifstream &fInput, double &P, double &i, int &t);
  13. double calculateFutureValue(double P, double i, int t);
  14. void writeDataToOutputFile(ofstream &fOutput, double F, double P, double i, int t);
  15.  
  16. //Driver function
  17. int main()
  18. {
  19. //Declare the needed variables
  20. ifstream fInput;
  21. ofstream fOutput;
  22. string nameOfInputFile, nameOfOutputFile;
  23. double P, i, F;
  24. int t;
  25. unsigned int result;
  26.  
  27. //Set precision
  28. cout << setprecision(2) << fixed << showpoint;
  29.  
  30. //Prompt for input file
  31. cout << "Enter the name of the input file: ";
  32.  
  33. //Read input file name
  34. cin >> nameOfInputFile;
  35.  
  36. //Open the input file
  37. fInput.open(nameOfInputFile.c_str());
  38.  
  39. //Check condition
  40. if (fInput.fail())
  41. {
  42. //Error message
  43. cout << "File \"" << nameOfInputFile << "\" could not be opened" << "\n";
  44. }
  45.  
  46. //Otherwise
  47. else
  48. {
  49. //Open a file for writing output
  50. fOutput.open("output.xls");
  51.  
  52. //Write to output file
  53. fOutput << "Future Value" << "\t" << "Present Value" << "\t" << "Monthly Interest" << "\t" << "Months" << endl;
  54.  
  55. //Loop to iterate and compute
  56. while (true)
  57. {
  58. //Assign result with the function return
  59. result = readDataFromFile(fInput, P, i, t);
  60.  
  61. //Check condition
  62. if (result == 1)
  63. {
  64. //Assign F with function return
  65. F = calculateFutureValue(P, i, t);
  66.  
  67. //Function call
  68. writeDataToOutputFile(fOutput, F, P, i, t);
  69. }
  70.  
  71. //Check condition
  72. else if (result == 2)
  73. {
  74. //Display
  75. cout << P << " " << i << " " << t << endl;
  76.  
  77. //Display
  78. cout << "One or more of the above values are not greater than zero\n";
  79.  
  80. //Continue
  81. continue;
  82. }
  83.  
  84. //Otherwise
  85. else
  86. {
  87. //Close input file
  88. fInput.close();
  89.  
  90. //Close output file
  91. fOutput.close();
  92.  
  93. //Stop
  94. break;
  95. }
  96. }
  97. }
  98.  
  99. //Pause
  100. cin.get(); cin.get();
  101.  
  102. //Return
  103. return 0;
  104. }
  105.  
  106. //Function definition - readDataFromFile()
  107. unsigned int readDataFromFile(ifstream &fInput, double &P, double &i, int &t)
  108. {
  109. //Check condition
  110. if (fInput >> P >> i >> t)
  111. {
  112. //Check condition
  113. if (P <= 0 || i <= 0 || t <= 0)
  114.  
  115. //Return
  116. return 2;
  117.  
  118. //Otherwise
  119. else
  120.  
  121. //Return
  122. return 1;
  123. }
  124.  
  125. //Otherwise
  126. else
  127. {
  128. //Return
  129. return 0;
  130. }
  131. }
  132.  
  133. //Function definition - calculateFutureValue()
  134. double calculateFutureValue(double P, double i, int t)
  135. {
  136. //Compute F
  137. double F = (P)*pow(1 + (i / 100), t);
  138.  
  139. //Return F
  140. return F;
  141. }
  142.  
  143. //Function definition - writeDataToOutputFile()
  144. void writeDataToOutputFile(ofstream &fOutput, double F, double P, double i, int t)
  145. {
  146. //Write to output file
  147. fOutput << F << "\t\t" << P << "\t\t" << i << "\t\t\t" << t;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement