samedi 18 avril 2020

Function in C++ programming, converting interest rate value before the call of a function present value?

#include<iomanip>

#include<cmath>

// #include<>

using namespace std;

double presentValue(double futureValue, double interestRate, int numberYears);

int main(){

    double futureValue, interestRate;

    int numberYears;

    cout<<"Enter future value\n";

    cin>>futureValue;

    while(futureValue<=0){

        cout<<"The future value must be greater than zero"<<endl;

        cout<<"Enter future value\n";

        cin>>futureValue;

    }

    cout<<"Enter annual interest rate\n";

    cin>>interestRate;

    while(interestRate<=0){

        cout<<"The annual interest rate must be greater than zero"<<endl;

        cout<<"Enter annual interest rate\n";

        cin>>interestRate;        

    }

    cout<<"Enter number of years"<<endl;

    cin>>numberYears;

    while(numberYears<=0){

        cout<<"The number of years must be greater than zero"<<endl;

        cout<<"Enter number of years"<<endl;

        cin>>numberYears;

    }
    double present_value = presentValue(futureValue, interestRate, numberYears);

    cout<<setprecision(2)<<fixed;

    cout<<"Present value: $"<<present_value<<endl;

    cout<<"Future value: $"<<futureValue<<endl;

    cout<<"Annual interest rate: "<<setprecision(3)<<interestRate<<"%"<<endl;

    cout<<"Years: "<<numberYears<<endl;

    return 0;
}
double presentValue(double futureValue, double interestRate, int numberYears){
    double r=interestRate/100;
    return futureValue / pow((1+r), numberYears);
}

The program is correct but I have to convert the interest rate ( /100 ) before the call of presentvalue function, I don't really know how to fix that.

Procedure: Note that the 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). This conversion needs to be done before you call the presentValue function. If you do the conversion in the presentValue function your program will fail the unit tests, so do the conversion before you call the calculate function.

Even though my program is correct this is the output I am getting because I did follow the procedure:Calling function presentValue The call to presentValue(22000.00, 0.01, 10) returned an unexpected value of 21980.21 Make sure you are coverting the interest rate before you call presentValue.

I just don't know if I have to do the conversion in the main function or else. I tried a lot of stuff but they were all wrong.

Aucun commentaire:

Enregistrer un commentaire