# Thread: Using an Exponent in C++

1. ## Using an Exponent in C++

Hey guys I am still really new to C++, as I am currently taking my first programming class. I am currently working on a payment calculator. I am using Visual Studio 2010. Before I get into this I want to say this is NOT homework just simply practice for me. This is what I have so far.

#include<iostream>
#include<math.h>
usingnamespace std;
intmain()
{

float rate, priceOfHouse, years, finalPayment;

const double MONTHLY_INTEREST_RATE = (rate/100/12);

const float INTIAL_MONTHLY_PAYMENT = (rate/100/12) * priceOfHouse;

const double LENGTH_OF_LOAN = -years/12;
cout <<
"Enter Interest Rate: ";
cin >> rate;
cout <<
"Enter Price of House: ";
cin >> priceOfHouse;
cout <<
"Length of Loan in terms of Years: ";
cin >> years;
finalPayment = INTIAL_MONTHLY_PAYMENT / (1 - ((1 + (MONTHLY_INTEREST_RATE)) exp(LENGTH_OF_LOAN)))

************************************************** ******
The problem that I am stuck at right now is this last line finalPayment =, the exponent (exp) part. It is telling me Error: expected a ")"
With the variables plugged in the formula is suppose to look like so: (input numbers are just expamples)
((6.5/100/12)*200000 / (1-((1+(6.5/100/12))^(-30/12)))

Not looking for anyone to finish this for me just a little help with this last line, maybe an example of what something like this (a formula) would look like in C++

Thanks,
NismoT

2. ((6.5/100/12)*200000 / (1-((1+(6.5/100/12))^(-30/12))))
==
Code:
`((6.5/100/12)*200000 / (1-pow(((1+(6.5/100/12)),(-30/12)))))`

3. You seem to think that
a^b = a exp(b)
which is not the case.
In C++, a^b = exp(a,b)

Take that into account when writing your code.

4. Originally Posted by Elysia
You seem to think that
a^b = a exp(b)
which is not the case.
In C++, a^b = exp(a,b)
It is not that either.

In C++ (and C) exp() is a function that accepts one argument. exp(a) computes e to the power of a, where e is the base for natural logarithms (2.718....)

If you want to compute a to the power of b, where both a and b are of type double, the form is pow(a, b). Bear in mind that some forms of raising a value to a power are trivial. In practice, it is often preferable to perform a*a rather than pow(a, 2.0), for example.

And a^b in C++ is a bitwise operation on integral values (which is why I've used words "to the power of" rather than a^b notation).

5. Right logic, wrong function, doh >_<

6. Yup.

7. Code:
```float rate, priceOfHouse, years, finalPayment;
const double MONTHLY_INTEREST_RATE = (rate/100/12);
const float INTIAL_MONTHLY_PAYMENT = (rate/100/12) * priceOfHouse;
const double LENGTH_OF_LOAN = -years/12;

cout << "Enter Interest Rate: ";
cin >> rate;
cout << "Enter Price of House: ";
cin >> priceOfHouse;
cout << "Length of Loan in terms of Years: ";
cin >> years;```
There is a problem with the order of these operations. At the point where you are initializing your constants, the variables rate/years/priceofHouse will have a random value. This random value will be used to initialize your constants. Later, reading into these variables does not magically change the values of the previously initialized constants based on the user's input. You need to get the user's input first and then calculate those constants.

Code:
`#include <math.h>`
You should probably use this instead:
Code:
`#include <cmath>`
...not that it's likely to matter much.

Popular pages Recent additions