Confused on a function??!!

Hello everyone.
Thanks for taking your time and trying to help me.

i have a function where i can't figure out the result of (i).

Please any advice or hint on how to solve this would be appreciated.

Again Thanks for the time.

My test numbers are:

Loan = $1000
Interest = 6 %
Payments a year = 12 payments
Total years loan = 3 years

The result should be $30.42

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
double monthlyPayment ()
{
	double loanAmount, rValue, i;
	int    r, m, t, n;

	cout <<"Enter the loan amount: ";
	cin  >> loanAmount;
	cout << endl;

	cout <<"Enter the interest per year as a number: ";
	cin  >> r;
	cout << endl;
		
	cout <<"Enter the payments in one year: ";
	cin  >> m;
	cout << endl;

	cout <<"Enter the number of years for the loan: ";
	cin  >> t;
	cout << endl;
	
        //the result of (i) is 0.00 i do not get how that is happening??!!	
	i = static_cast <double> (i = r/(m*100));
	cout << "test " << i << endl; 

	n = m*t;
	cout <<"test2 " << n << endl;

	rValue = loanAmount * i / (1 - pow(1+i,-n));

        //This is a test only output statment
	//cout <<"The monthly payment is: $ " << rValue << ".\n\n";

 return rValue;
}
Last edited on
why are you using static_cast rather than just:

i = r/(m*100);

i think i will get a warning in loss of data from int to double?
Use it like that?
i = static_cast <double> ( r/(m*100));
i tried it does not show a warning but the result is still the same.

but i do have:
1
2
int r, m;
double i;


I think i need to convert it right ?
But you are declaring i as a double?

double loanAmount, rValue, i;


So therefore you should be able to use the code I gave earlier?
This works.
i = ( double(r)/(double(m)*100));
closed account (D80DSL3A)
The problem is that you are dividing integers. In integer division the / operator gives the quotient.eg. 2/5 = 0 NOT 0.4
The % operator gives the remainder. eg. 2%5 = 2

You are getting 0 because r < 100*m.
Easy fix is to make all your variables doubles.

Other way:
i = static_cast <double>(r)/static_cast <double>(100*m);
when i get the user input i would rather have it as whole number (int).

And if it is declared as a double it will stop the program from runing properly i think.

I am a begginer so forgive me if i am saying smth. that does not make sence.

Thank you for your help

I wll try your way though.
Last edited on
Thanks oldnewbie and fun2code

I do understand it now.

But i am curious:
Can anyone of you guys explain the difference between the

oldnewbiew way i=( double(r)/(double (m) * 100));

and
fun2code way i= statc_cast <double> (r)/static_cast <double>(m*100);

Thanks againg and keep up the good work.
As I know,there is no difference,except that in a long listing of code,it is easier to find "static_cast " keywords in case of debugging and finding errors.(since casting my cause data loss and error)
thanks for making that clear.

I encountered a problem with the function above.

In C++ can we get a input from the user as an int and print out the result.

And then an input as a double and still print out the result?

I ran my program and put as my (r) a 5.25 percent and it skips some steps and the result is not even close.

Thanks for your advice.
Last edited on
Of course you can print out a double value as you do int values.
But in the above code you defined r as integer.If you want to enter values such as 5.25 define it as [b]double.[/b]
Last edited on
What oldnewbie is doing is creating temporary doubles from the integer values of r and m. He then performs the necessary operations, multiplies the result by an integer (careful...), and then the doubles are discarded.

What fun2code is going is using a static_cast, the details of the implementation of which I am not 100% certain of. static_cast is good for conversions of integral types, and it's less restrictive and has less of an overhead than dynamic_cast (which is safer, by the way).

-Albatross
thanks everyone.
Topic archived. No new replies allowed.