Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Nov 15th, 2018 102 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. // the purpose of this program is to calculate the future value you would get based on  a certain present value
  2. #include <iostream>
  3. #include <fstream>                                  //header files, fstream to use files, cmath to use the pow function, and iomanip for output formatting
  4. #include <cmath>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. int readFile(ifstream& infile, double& presentValue, double& interestRate, int& months);
  10. double calculateFutureValue(double presentValue, double& interestRate, int months);                             //function prototyping so main can be the first
  11. void writeFile(ofstream& outfile, double presentValue, double interestRate, int months, double futureValue);    // function
  12.  
  13. int main()
  14. {
  15.  
  16.  
  17.    string fileName;
  18.    cin >> fileName;
  19.                                                            // get input from a file typed by the user
  20.    ifstream infile;
  21.    infile.open(fileName);
  22.  
  23.    if (infile)                                                                              // if the file was correctly opened perform the following actions
  24.    {
  25.  
  26.       double presentVal;
  27.       double intRate;
  28.       int months;                                               //variables to store values in and then use in the output file
  29.       int read;
  30.       double futureVal;
  31.  
  32.       ofstream outfile;
  33.       outfile.open("output.xls");                                           // create and open a file called output.xls, .xls is excel sheet
  34.  
  35.       if (outfile)                                                                          //if the outfile opened correctly perform the following actions
  36.       {
  37.  
  38.          outfile << "Future Value\tPresent Value\tMonthly Interest\tMonths" << endl;            // always output this statement at the beginning of the file
  39.          while (infile >> presentVal)
  40.          {
  41.             read = readFile(infile, presentVal, intRate, months);                           //calling the read function
  42.             if (read == 1)                                                                  // if read is true perform the other functions
  43.             {
  44.                futureVal = calculateFutureValue(presentVal, intRate, months);
  45.                writeFile(outfile, presentVal, intRate, months, futureVal);
  46.             }
  47.             else
  48.             {
  49.                intRate = intRate * 100;
  50.  
  51.                cout << setprecision(2) << fixed;                                                    //else tell the user that one of the values was invalid
  52.                cout << presentVal << " " << intRate << " ";
  53.                cout << setprecision(0) << fixed << months << endl;
  54.                cout << "One or more of the above values are not greater than zero" << endl;
  55.             }
  56.  
  57.          }
  58.       }
  59.       else
  60.       {
  61.          cout << "File \"output.xls\" could not be opened" << endl;
  62.          infile.close();                                                            // if the file could not be opened correctly tell the user and close the file
  63.          return 0;
  64.       }
  65.  
  66.       infile.close();
  67.    }
  68.    else
  69.    {
  70.       cout << "File \"" << fileName << "\" could not be opened" << endl;
  71.    }
  72.  
  73.    return 0;
  74. }
  75.  
  76.  
  77.  
  78. int readFile(ifstream& infile, double& presentValue, double& interestRate, int& months) //read in function for files
  79. {
  80.    unsigned int end;
  81.  
  82.  
  83.    infile >> interestRate;
  84.    interestRate = interestRate / 100;                                           //read in the values and convert interest rate to percentage
  85.    infile >> months;
  86.  
  87.    if (presentValue > 0 && interestRate > 0 && months > 0)
  88.    {
  89.       end = 1;                                                                  // this will indicate if the values were read properly
  90.    }
  91.    else if (presentValue <= 0 || interestRate <= 0 || months <= 0)
  92.    {
  93.       end = 2;                                                                  // this will indicate if there was an invalid value
  94.    }
  95.    else
  96.    {
  97.       end = 0;                                                                  // this will indicate that the program reached the end of the file
  98.    }
  99.  
  100.    return end;                                              // return one of those values
  101. }
  102.  
  103. double calculateFutureValue(double presentValue, double& interestRate, int months)  // calculation function
  104. {
  105.    double futureValue;
  106.  
  107.    futureValue = presentValue * pow((1 + interestRate), months);            //calculation for future value
  108.    interestRate = interestRate * 100;                                       // after calculations, return interest rate to its original value
  109.  
  110.    return futureValue;
  111. }
  112.  
  113. void writeFile(ofstream& outfile, double presentValue, double interestRate, int months, double futureValue) //output function for files
  114. {
  115.  
  116.  
  117.    outfile << setprecision(2) << fixed;                                                                         //setting the values to decimal value
  118.    outfile << futureValue << "\t" << presentValue << "\t" << interestRate << "\t";
  119.    outfile << setprecision(0) << fixed << months << endl;                                                           //outputting the values to a file
  120. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top