Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<cmath>
- #include<string>
- #include<iomanip>
- using namespace std;
- //Function to calculate the future value and return the same
- double calculateFutureValue(double presentValue, double interestRate, int months){
- double futureValue = (double)presentValue * pow( (1 + interestRate), months);
- return futureValue;
- }
- //Function to read the input file and assign the value to the variables
- unsigned int readfile(ifstream &inF, double &presentValue, double &interestRate, int &months){
- inF >> presentValue;
- inF >> interestRate;
- inF >> months;
- if(presentValue <= 0 || interestRate <= 0 || months <= 0){
- return 2;
- }
- else if( !(presentValue <= 0 || interestRate <= 0 || months <= 0)){
- return 1;
- }
- else{
- if(inF.eof()){
- return 0;
- }
- }
- }
- //Function to write data to file
- void write2File(ofstream &of, double futureValue, double presentValue, double interestRate, int months){
- of << futureValue << "\t" << presentValue << "\t" << interestRate << "\t" << months << "\n";
- }
- // main function
- int main(void){
- string infile;
- cout << "Enter the filename you want to process: ";
- cin >> infile;
- cout << endl;
- ifstream inF;
- inF.open(infile.c_str());
- if(!inF){
- cout << "File " << infile << " could not be opened" <<endl;
- return -1;
- }
- string outfile = "output.xls";
- ofstream of;
- of.open(outfile.c_str());
- if(!of){
- cout << "Error in creating the output file" << endl;
- return -1;
- }
- of << "Future Value\tPresent Value\tMonthly Interest\tMonths\n"; // printing header to output file
- double futureValue;
- double presentValue;
- double interestRate;
- int months;
- of << fixed << setprecision(2); // to print 2 places after decimal into file
- while(!inF.eof()){ // looping untill end of file
- int returnVal = readfile(inF, presentValue, interestRate, months);
- if(returnVal == 2){
- cout << presentValue << "\t" << interestRate << "\t" << months << endl;
- cout << "One or more of the above values are not greater than zero." << endl;
- }
- else if(returnVal == 1){
- futureValue = calculateFutureValue(presentValue, interestRate/100, months);
- write2File(of, futureValue, presentValue, interestRate, months);
- }
- }
- inF.close();
- of.close();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment