I shouldn't need a 'double' ??

Hi. I am writing a program that analyzes horse races. Anyway, at the start I need to include only those distances that I deem 'playable'. They include races from lengths of 6 furlongs to 9 furlongs.(a furlong is an eighth of a mile). Any length not in the above parameters gets booted, and the program ends.
I typed this variable as 'float lengthOfRace'. No problem. I am able to work with 6.5 furlongs, 7 furlongs, 8.5 furlongs; but when I go to 1 mile and 40 yards(8.2 furlongs) and 1 mile and 70 yards(8.3 furlongs) I have to type the variable lengthOfRace as double. Otherwise I get kicked out of my program.
Here is 'part' of the function lengthOfRaceFunction.

void lengthOfRaceFunction()
{
//this function also calls all other functions

cout << "\nEnter Length Of Race(99 to quit): ";
cin >> lengthOfRace;

cout << "\n\nlengthOfRace: " << lengthOfRace << endl;

if (lengthOfRace == 99)
{
return;
}

if (lengthOfRace < 6 || lengthOfRace > 9)
{
cout << "\nWe don't do races less than 6 furlongs, ";
cout << "or greater than 9 furlongs. \n";
return;
}

if (lengthOfRace != 6 & lengthOfRace != 6.5 & lengthOfRace !=7 &
lengthOfRace != 8 & lengthOfRace != 8.2 & lengthOfRace != 8.3 &
lengthOfRace != 8.5 & lengthOfRace != 9)
{
cout << "\ntryAgain!";
lengthOfRaceFunction();
}
//rest of function I think is not applicable.

Why does 8.5 furlongs work as a float, but 8.2 and 8.3 don't????
Using floats should work, floats can take those values. where do you declare lengthOfRace? also is there a reason why you use bitwise & in you if? Also as a side note your if( lengthOfRace == 99 ) will give you warnings with some compilers. Use a > operator, as == with floats and doubles practically never works.
You're comparing floating point values for equality. That always gives problems.
floats and doubles aren't very good at storing certain values due to the inherent problems of the binary system. For example, 1/10 can't be expressed as a binary number in finite space (compare with how you can't express 1/3 as a decimal number in finite space). Instead, an approximation is used.
The problem in this case is that the approximation you're using in the code is for double (8.2 is a double, 8.2f is a float), which is a different magnitude than that for float, just like how 0.333 and 0.333333 are both approximations of 1/3 of different precision, and ultimately different numbers.

I would suggest using, say, 10 to represent 1, 15 to represent 1.5, and so on. Floats aren't really designed for this kind of work.
reply to first reply:
{
Using floats should work, floats can take those values. where do you declare lengthOfRace? also is there a reason why you use bitwise & in you if? Also as a side note your if( lengthOfRace == 99 ) will give you warnings with some compilers. Use a > operator, as == with floats and doubles practically never works.
}
lengthOfRace is declared globally.(I use it in a couple of functions) I use '&' to make sure that the program won't go any further when an undesired distance (ex. 5 furlongs) is put in, or a mistake happens(ex. 87 furlongs).

reply to second reply:
{
I would suggest using, say, 10 to represent 1, 15 to represent 1.5, and so on. Floats aren't really designed for this kind of work.
}
I thought of doing that, but I wanted program to be as user friendly as possibe. I was thinking of: 'enter 1 for 6f, 2 for 6.5f, 3 for 7f,' etc. or it might be easier to add a zero as per your suggestion.
Then what you can do is input into a string, strip the dots, and convert that to an integer.
"1.7" -> "17" -> 17
Topic archived. No new replies allowed.