Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1.  
  2.  
  3. #include<iostream>
  4.  
  5. #include<fstream>
  6.  
  7. #include<cmath>
  8.  
  9. #include<string>
  10.  
  11. #include<iomanip>
  12.  
  13. using namespace std;
  14.  
  15. //Function to calculate the future value and return the same
  16.  
  17.  
  18.  
  19. double calculateFutureValue(double presentValue, double interestRate, int months){
  20.  
  21. double futureValue = (double)presentValue * pow( (1 + interestRate), months);
  22.  
  23. return futureValue;
  24.  
  25. }
  26.  
  27. //Function to read the input file and assign the value to the variables
  28.  
  29.  
  30.  
  31. unsigned int readfile(ifstream &inF, double &presentValue, double &interestRate, int &months){
  32. inF >> presentValue;
  33. inF >> interestRate;
  34. inF >> months;
  35.  
  36. if(presentValue <= 0 || interestRate <= 0 || months <= 0){
  37.  
  38. return 2;
  39.  
  40. }
  41.  
  42. else if( !(presentValue <= 0 || interestRate <= 0 || months <= 0)){
  43.  
  44. return 1;
  45. }
  46. else{
  47.  
  48. if(inF.eof()){
  49.  
  50. return 0;
  51. }
  52.  
  53. }
  54.  
  55. }
  56.  
  57. //Function to write data to file
  58.  
  59. void write2File(ofstream &of, double futureValue, double presentValue, double interestRate, int months){
  60. of << futureValue << "\t" << presentValue << "\t" << interestRate << "\t" << months << "\n";
  61. }
  62.  
  63. // main function
  64.  
  65. int main(void){
  66. string infile;
  67. cout << "Enter the filename you want to process: ";
  68. cin >> infile;
  69. cout << endl;
  70. ifstream inF;
  71. inF.open(infile.c_str());
  72. if(!inF){
  73. cout << "File " << infile << " could not be opened" <<endl;
  74. return -1;
  75. }
  76. string outfile = "output.xls";
  77. ofstream of;
  78. of.open(outfile.c_str());
  79. if(!of){
  80. cout << "Error in creating the output file" << endl;
  81. return -1;
  82. }
  83. of << "Future Value\tPresent Value\tMonthly Interest\tMonths\n"; // printing header to output file
  84. double futureValue;
  85. double presentValue;
  86. double interestRate;
  87. int months;
  88. of << fixed << setprecision(2); // to print 2 places after decimal into file
  89. while(!inF.eof()){ // looping untill end of file
  90. int returnVal = readfile(inF, presentValue, interestRate, months);
  91.  
  92. if(returnVal == 2){
  93.  
  94. cout << presentValue << "\t" << interestRate << "\t" << months << endl;
  95.  
  96. cout << "One or more of the above values are not greater than zero." << endl;
  97.  
  98. }
  99.  
  100. else if(returnVal == 1){
  101.  
  102. futureValue = calculateFutureValue(presentValue, interestRate/100, months);
  103.  
  104. write2File(of, futureValue, presentValue, interestRate, months);
  105.  
  106. }
  107.  
  108. }
  109.  
  110. inF.close();
  111.  
  112. of.close();
  113.  
  114. return 1;
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement