Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // the purpose of this program is to calculate the future value you would get based on a certain present value
- #include <iostream>
- #include <fstream> //header files, fstream to use files, cmath to use the pow function, and iomanip for output formatting
- #include <cmath>
- #include <iomanip>
- using namespace std;
- int readFile(ifstream& infile, double& presentValue, double& interestRate, int& months);
- double calculateFutureValue(double presentValue, double& interestRate, int months); //function prototyping so main can be the first
- void writeFile(ofstream& outfile, double presentValue, double interestRate, int months, double futureValue); // function
- int main()
- {
- string fileName;
- cin >> fileName;
- // get input from a file typed by the user
- ifstream infile;
- infile.open(fileName);
- if (infile) // if the file was correctly opened perform the following actions
- {
- double presentVal;
- double intRate;
- int months; //variables to store values in and then use in the output file
- int read;
- double futureVal;
- ofstream outfile;
- outfile.open("output.xls"); // create and open a file called output.xls, .xls is excel sheet
- if (outfile) //if the outfile opened correctly perform the following actions
- {
- outfile << "Future Value\tPresent Value\tMonthly Interest\tMonths" << endl; // always output this statement at the beginning of the file
- while (infile >> presentVal)
- {
- read = readFile(infile, presentVal, intRate, months); //calling the read function
- if (read == 1) // if read is true perform the other functions
- {
- futureVal = calculateFutureValue(presentVal, intRate, months);
- writeFile(outfile, presentVal, intRate, months, futureVal);
- }
- else
- {
- intRate = intRate * 100;
- cout << setprecision(2) << fixed; //else tell the user that one of the values was invalid
- cout << presentVal << " " << intRate << " ";
- cout << setprecision(0) << fixed << months << endl;
- cout << "One or more of the above values are not greater than zero" << endl;
- }
- }
- }
- else
- {
- cout << "File \"output.xls\" could not be opened" << endl;
- infile.close(); // if the file could not be opened correctly tell the user and close the file
- return 0;
- }
- infile.close();
- }
- else
- {
- cout << "File \"" << fileName << "\" could not be opened" << endl;
- }
- return 0;
- }
- int readFile(ifstream& infile, double& presentValue, double& interestRate, int& months) //read in function for files
- {
- unsigned int end;
- infile >> interestRate;
- interestRate = interestRate / 100; //read in the values and convert interest rate to percentage
- infile >> months;
- if (presentValue > 0 && interestRate > 0 && months > 0)
- {
- end = 1; // this will indicate if the values were read properly
- }
- else if (presentValue <= 0 || interestRate <= 0 || months <= 0)
- {
- end = 2; // this will indicate if there was an invalid value
- }
- else
- {
- end = 0; // this will indicate that the program reached the end of the file
- }
- return end; // return one of those values
- }
- double calculateFutureValue(double presentValue, double& interestRate, int months) // calculation function
- {
- double futureValue;
- futureValue = presentValue * pow((1 + interestRate), months); //calculation for future value
- interestRate = interestRate * 100; // after calculations, return interest rate to its original value
- return futureValue;
- }
- void writeFile(ofstream& outfile, double presentValue, double interestRate, int months, double futureValue) //output function for files
- {
- outfile << setprecision(2) << fixed; //setting the values to decimal value
- outfile << futureValue << "\t" << presentValue << "\t" << interestRate << "\t";
- outfile << setprecision(0) << fixed << months << endl; //outputting the values to a file
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement