Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Code to copy
- //Include the needed header files
- #include <fstream>
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <cmath>
- using namespace std;
- //Function declarations
- unsigned int readDataFromFile(ifstream &fInput, double &P, double &i, int &t);
- double calculateFutureValue(double P, double i, int t);
- void writeDataToOutputFile(ofstream &fOutput, double F, double P, double i, int t);
- //Driver function
- int main()
- {
- //Declare the needed variables
- ifstream fInput;
- ofstream fOutput;
- string nameOfInputFile, nameOfOutputFile;
- double P, i, F;
- int t;
- unsigned int result;
- //Set precision
- cout << setprecision(2) << fixed << showpoint;
- //Prompt for input file
- cout << "Enter the name of the input file: ";
- //Read input file name
- cin >> nameOfInputFile;
- //Open the input file
- fInput.open(nameOfInputFile.c_str());
- //Check condition
- if (fInput.fail())
- {
- //Error message
- cout << "File \"" << nameOfInputFile << "\" could not be opened" << "\n";
- }
- //Otherwise
- else
- {
- //Open a file for writing output
- fOutput.open("output.xls");
- //Write to output file
- fOutput << "Future Value" << "\t" << "Present Value" << "\t" << "Monthly Interest" << "\t" << "Months" << endl;
- //Loop to iterate and compute
- while (true)
- {
- //Assign result with the function return
- result = readDataFromFile(fInput, P, i, t);
- //Check condition
- if (result == 1)
- {
- //Assign F with function return
- F = calculateFutureValue(P, i, t);
- //Function call
- writeDataToOutputFile(fOutput, F, P, i, t);
- }
- //Check condition
- else if (result == 2)
- {
- //Display
- cout << P << " " << i << " " << t << endl;
- //Display
- cout << "One or more of the above values are not greater than zero\n";
- //Continue
- continue;
- }
- //Otherwise
- else
- {
- //Close input file
- fInput.close();
- //Close output file
- fOutput.close();
- //Stop
- break;
- }
- }
- }
- //Pause
- cin.get(); cin.get();
- //Return
- return 0;
- }
- //Function definition - readDataFromFile()
- unsigned int readDataFromFile(ifstream &fInput, double &P, double &i, int &t)
- {
- //Check condition
- if (fInput >> P >> i >> t)
- {
- //Check condition
- if (P <= 0 || i <= 0 || t <= 0)
- //Return
- return 2;
- //Otherwise
- else
- //Return
- return 1;
- }
- //Otherwise
- else
- {
- //Return
- return 0;
- }
- }
- //Function definition - calculateFutureValue()
- double calculateFutureValue(double P, double i, int t)
- {
- //Compute F
- double F = (P)*pow(1 + (i / 100), t);
- //Return F
- return F;
- }
- //Function definition - writeDataToOutputFile()
- void writeDataToOutputFile(ofstream &fOutput, double F, double P, double i, int t)
- {
- //Write to output file
- fOutput << F << "\t\t" << P << "\t\t" << i << "\t\t\t" << t;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement