dimanche 26 avril 2020

Trying to figure out how to convert the interest rate before calling a function in C++

#include<iostream>

#include<fstream>

#include<iomanip>

#include<cmath>

using namespace std;
double futureValue(double presentValue, double interestRate, int months);
int readData(ifstream &inFile, double &presentValue, double &interestRate, int &months);
//method to read one set of data from input file

int main(){
                string input;

                cout<<"Enter input file name\n";

                //reading input file name

                cin>>input;

                //opening input file

                ifstream inFile(input.c_str());

                //if file is not opened correctly, displaying error and quitting

                if(!inFile){

                                cout<<"File \""<<input<<"\" could not be opened"<<endl;

                                return 0;
                }

                //opening output.xls file to write

                ofstream outFile("output.xls");

                //if file is not opened correctly, displaying error and quitting

                if(!outFile){

                                cout<<"File \"output.xls\" could not be opened"<<endl;

                                return 0;

                }

                cout<<setprecision(3)<<fixed;

                outFile<<setprecision(2)<<fixed;        

                //writing heading to outFile

                outFile<<"Future Value\tPresent Value\tMonthly Interest\tMonths"<<endl;
                double presentVal, futureVal, monthlyInterest;
                int months;

                //reading first set of values from input file

                int readStatus=readData(inFile,presentVal,monthlyInterest, months);

                //looping as long as readData method not return 0

                while(readStatus!=0){

                                //if readStatus is 1, finding future value, writing data to outFile

                                if(readStatus==1){

                                                futureVal=futureValue(presentVal,monthlyInterest,months);

                                                outFile<<setprecision(2)<<futureVal<<"\t"<<setprecision(2)<<presentVal<<"\t"<<setprecision(3)<<monthlyInterest<<"\t"<<months<<endl;

                                }else{

                                               //otherwise displaying data to cout and printing error message

                                               cout<<presentVal<<" "<<monthlyInterest<<" "<<months<<endl;

                                               cout<<"One or more of the above values are not greater than zero"<<endl;

                                }

                                //reading next set of data

                                readStatus=readData(inFile,presentVal,monthlyInterest, months);

                }

                //closing files before exiting

                inFile.close();

                outFile.close();

                return 0;

}

//second
int readData(ifstream &inFile, double &presentValue, double &interestRate, int &months){

                //reading values into the reference variables

                if(inFile>>presentValue>>interestRate>>months){

                                //if values are valid, returning 1, else returning 2

                                if(presentValue>0 && interestRate>0 && months>0){

                                               return 1;

                                }

                                return 2;

                }else{

                                //if reading failed (end of file), returning 0

                                return 0;

                }

}

//method to calculate the future value using the given equation
double interestRate=interestRate/100;
double futureValue(double presentValue, double interestRate, int months){
                return presentValue*pow(1+interestRate,months);

}

Here is the directive: Note that the monthly interest rate will be a number such as 10 or 12.5. These are to be read in as percentages (10% and 12.5%). You will need to divide these values by 100 to convert them into the values needed in the function (.1 and .125 for the above values). You need to do this conversion before you call the futureValue function

if I do the conversion before I call the function futurevalue I am not getting the value that I want for future value. I am looking for a way to convert the interest before calling the function and get the correct answer at the same time. The way my code looks right now the function futurevalue is returning an incorrect value because the interest rate is still in percentage.

Example: this is a correct input:input.txt

The input.txt file contains:

100 .99 36
The contents of output.xls after the run:

Future Value    Present Value   Monthly Interest    Months
142.57    100.00  0.990   36 

Aucun commentaire:

Enregistrer un commentaire