0

I am in need of help to compute a present value. The user will input: payment amount (payamt), term (trm), and interest rate (intrte). I am having trouble computing the following equation:

present value = payment amount * ((1-(1+interest)^-term)/interest rate)

This is my code so far:

#include <iostream>
using namespace std;

int main()

{
int trm = 0
    ;

double intrte = 0.0
    ;

float payamt = 0.0,
    presVal = 1
    ;

char
    response = '\0'
    ;

    cout << "Would you like to compute a present value? Enter Y for yes; N for no.";
    cin >> response;

    if (response != 'Y') {
        return 0;
    }

    cout << "\nPayment Amount: $";                  //payment in $ and cents for each year
    cin >> payamt;

    cout << "\nTerm (in years): ";                  //term number of years of payments
    cin >> trm;

    cout << "\nInterest Rate (between 0 and 100): ";//interest rate
    cin >> intrte;

    cout << "\n\nThe present value for a payment amount of $" << payamt
    << " and an interest rate of " << intrte
    << "%, and a term of " << trm
    << " years is $" << presVal
    << ".\n\n" << endl;

    presVal == payamt * (1 - ((1 + intrte), (-trm)))/intrte;
}
  • 1
    Can you elaborate on what issue you are having? Compile error? Run time error? And what the error messages are? – Anon Mail Mar 18 '20 at 13:43
  • on your last line, you're comparing presVal by using "==", you should use "=" if you want to assign te formula to presVal – Arno Deceuninck Mar 18 '20 at 13:51
  • Does this answer your question? [What is the C++ function to raise a number to a power?](https://stackoverflow.com/questions/845912/what-is-the-c-function-to-raise-a-number-to-a-power) – Ardent Coder Mar 18 '20 at 14:02

1 Answers1

1

When you include cmath (#include <cmath>), you can use the double pow(double base, double exponent) function to take a number to the power of another number. More info can be found here: http://www.cplusplus.com/reference/cmath/pow/

Your formula becomes then: presVal = payamt * (1 - pow(1+intrte, -trm))/intrte;

Arno Deceuninck
  • 320
  • 3
  • 10
  • Thank you! I am instructed to not use any other header files other than iomanip. Can you advise? Thank you again! MUCH appreciated! – mknitter718 Mar 18 '20 at 14:06
  • You can write your own pow function, which implements a for loop which iterates exponent times and multiplies each iteration the result (which is initialized on 1) with the base, and returns the result at the end of the function – Arno Deceuninck Mar 18 '20 at 14:22
  • What do you mean "write your own pow function"? Sorry, I am a beginner :) – mknitter718 Mar 18 '20 at 14:30
  • I added cmath and the formula you provided. I am still receiving "1" as the present value. I think that is because it is defined in the variables as "presVal = 1". Do I need to create a "for" loop? Thank you! – mknitter718 Mar 18 '20 at 14:45
  • You're still getting 1 because you're calculating your presVal after you've outputed it's value. You should first calculate it and then output it. A for loop is required for calculating the power without including cmath. (asuming trm is a natural number) – Arno Deceuninck Mar 18 '20 at 14:55
  • 1
    WOW! Thank you SO SO much for your help! – mknitter718 Mar 18 '20 at 15:45