Oct 27, 2013 at 4:21pm UTC
Hello!
I tried to convert double number m to integer.
Problem is, program is automatically converting p also to integer!
But I wrote just m to be converted to integer, not p!
Please, could someone help me get along with the mess?
Many thanks!
1 2 3
double p3=d3*100/s;
double m3= p3*s/100;
int b3=(int )m3;
Last edited on Oct 27, 2013 at 4:22pm UTC
Oct 27, 2013 at 7:52pm UTC
Your question isn't clear. What are d3
and s
Oct 28, 2013 at 1:10pm UTC
I am counting a procent.
(ie. we have d3 = 3 red apples, s= all the apples, p3= % red apples from all the apples)
d3 from the whole amount of s is a procent (p3)
problem is just that I converted to integer double m3 and not double p3.
But when I wrote cout << p3;
then p3 was alo rounded to a n integer.
But with the expression
int b3=(int)m3;
I thought, I converted jsut doube m3 to integer, not also p3.
it means, that , somehow automaticaly, p3 also "converted " from double to integer.
is that because they are both "connected" withe the sedond expression? I did not know that shoul dhave some influence, cause that is the row BEFORE.
many thanks!
Last edited on Oct 28, 2013 at 1:11pm UTC
Oct 28, 2013 at 1:32pm UTC
I believe since you're using both integers and doubles in the same equation, it is causing an issue.
Look for a warning message when you compile your program that indicates a conversion problem.
You might want to use all doubles and setprecision(0) for the numbers you want ?
Someone else should confirm what I'm saying, I'm still a noob but I had a similar problem just last night and solved it that way.
Oct 28, 2013 at 1:37pm UTC
Setprecision() works for the output streams , doubles have as much precision as the mantissa can hold.
Try using all doubles, and writing 100.0 instead of just 100.
Oct 28, 2013 at 1:47pm UTC
I think the previous comments probably give the correct answer.
My question about d3
and s
, I really meant, what type are they, double
or int
or something else.
But 100
is definitely of type int
. In this case it would probably be better to use 100.0
instead, which is type double
.