I don't know how to illustrate the problem any more simple.
It works with int values but not with float. When I change to float it just always returns 0.00.
I can't see the problem. :(
Printable View
I don't know how to illustrate the problem any more simple.
It works with int values but not with float. When I change to float it just always returns 0.00.
I can't see the problem. :(
Even if I just set ratio to say 2 and comment out the division, it still returns a 0.00 so it's not a casting error.
Take for example this program:Quote:
I don't know how to illustrate the problem any more simple.
It works for me, and differs only very slightly from the gist of your program.Code:#include <stdio.h>
float comStaRatio(int comments, int statements)
{
float ratio=1;
ratio=(float)comments/(float)statements;
return(ratio);
}
int comCount()
{
return 4;
}
int staCount()
{
return 3;
}
int main(void)
{
printf("Comment to statement ratio: %.2f\n", comStaRatio(comCount(), staCount()));
return 0;
}
Could it be to do with my compiler?
What compiler are you using? Does my code example work for you? The expected output is:Quote:
Could it be to do with my compiler?
Comment to statement ratio: 1.33
>Could it be to do with my compiler?
It's certainly possible, but the chances of you finding a bug in a mature compiler without wading through some serious edge cases of the language are vanishingly small. What compiler are you using?
I am using Bloodshed Dev-C++
I found something weird... If I print ratio within the function, it works fine but still returns 0.00
With what input values?Quote:
If I print ratio within the function, it works fine but still returns 0.00
Again, does my example work for you?
Sorry, yes it works fine!
comments:2
statements: 8
therefore .25
Well, in that case it cannot be some kind of compiler or configuration problem.Quote:
Sorry, yes it works fine!
You also can see that it does work with float. Therefore, the error must lie somewhere else in your code.
I think I know what it might be. I am reading a file as the parameter of both functions within the comStaRatio function and therefore I think the file isn't undergoing rewind() for the second function, no?
Wait that can't be it. oops
I do not have the code and I do not have the executable, so I have no idea. Try your fix out and see.Quote:
I am reading a file as the parameter of both functions within the comStaRatio function and therefore I think the file isn't undergoing rewind() for the second function, no?
It's ok i've given up trying to return a value so i've made the function void and just printing the line within the function itself rather than main.
Thank you so much for your help laserlight!
Due to level of indirection it may create problem..
Try this..
ratio=(int) comments/(int) statements;