# Help with typecasting

• 09-14-2009
netto
Help with typecasting
The assignment asks to input these values:

D = Investment Amount (5000)
P = Interest (5.25)
Y = Number of Years (2.25)
M = Conversions/Year (4)

It tells me to use float for interest, and int for the rest.
I have to use this formula to calculate the Maturity value (S)
S = D ( 1 + P/M) to the power of YM

Using the data above S is suppose to equal 5622.60
When I debug I get 5065.62.

I think I have made an error in typecasting int to double or float to double

Code:

```// MaturityCalc.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <math.h> using namespace std; int _tmain(int argc, _TCHAR* argv[]) {         int m, y, d;         double s;         float p;         //keyboard input         cout << "Please input the following information";         cout << endl;         cout << "Investment Amount: ";         cin >> d;         cout << endl;         cout << "Interest Rate: ";         cin >> p;         cout << endl;         cout << "Conversions/Year: ";         cin >> m;         cout << endl;         cout << "Number of Years: ";         cin >> y;         cout << endl;         //end keyboard entry                 y = y * m;         p = 1 + (p/100)/m;         pow ((double) p , (double)y);         s = d * p;         cout << "This is the result: " << (double)s;         system ("pause");         return 0; }```

Note: This is my second assignment, first week of C++, I have very little knowledge of this.

Any help is appreciated.
• 09-14-2009
Dino
If you have 2.25 years, then years needs to be a float or double.
• 09-14-2009
netto
Thanks for that, it fixed a glitch that was skipping y, but I'm still getting the same answer.
• 09-14-2009
Dino
Are you getting (and ignoring) any compiler warnings?

Did you fix the data type or just cast around it?
• 09-14-2009
netto
No compiler errors. I'm not sure what your second comment means, sorry.
• 09-14-2009
Dino
```// MaturityCalc.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <math.h> using namespace std; int _tmain(int argc, _TCHAR* argv[]) {         int m, d;         double s;         float p, y;         //keyboard input         cout << "Please input the following information";         cout << endl;         cout << "Investment Amount: ";         cin >> d;         cout << endl;         cout << "Interest Rate: ";         cin >> p;         cout << endl;         cout << "Conversions/Year: ";         cin >> m;         cout << endl;         cout << "Number of Years: ";         cin >> y;         cout << endl;         //end keyboard entry                 y = y * m;         p = 1 + (p/100)/m;         pow ((double) p , (double)y);         s = d * p;         cout << "This is the result: " << (double)s;         system ("pause");         return 0; }```