Reputation: 7878
I'm making a cricket game so if you have a little idea about cricket you'll easily understand this problem.
I'm stuck at a logic which I cannot figure out. It is regarding total numbers of overs. (1 over means player will throw 6 balls, same 2 overs means 12 balls and so on).
I've been able to get if over is 1
balls left are 6
. And to show how many overs are done I have to show it in a form:
0.4
<-(4th ball of 1st over) or 1.3
<-(3rd Ball of 2nd over)
similarly when the number reaches 0.6
it will be equal to 1
So I've the information in int
and want to display it as float
. Any logical advice of how can I achieve this.
Upvotes: 1
Views: 55
Reputation: 24417
Don't use floats at all, because you could get floating point errors. For storing a discrete quantity, use an int. Store the total number of balls as an int and when you want to display it, divide by 6 to get overs and take modulo 6 to get remainder (which ball of the over it is). Add 1 if you want to number your balls within the over starting from 1 instead of 0.
int balls = 62; // example value
printf("%d.%d", balls / 6, (balls % 6) + 1);
Should output 10.3 (third ball of 11th over)
When balls is zero it is the first ball of the first over (0.1).
If you insist on having a float, you can convert like this:
float overs = (float)(balls / 6) + ((float)((balls % 6) + 1) * 0.1f);
Upvotes: 3