English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am trying to count the amount of numbers(decimals: x/100) between another set of decimals EX: (0.0 and 0.1) and total them in a variable called pointOne and I'm not sure why it isn't working. Here's a code segment:

if((x/100) >= 0.00 || (x/100) <= 0.10)
{
pointONE = pointONE++;
}
This continues for the numbers 0.0 to 0.9 (contained in if statements) and at the end of the if statements prints the amount of numbers for every given range. Any idea what is wrong?

2006-09-25 05:32:44 · 3 answers · asked by Reaper0886 1 in Computers & Internet Programming & Design

3 answers

Is x declared as an integer?

I am thinking it probably is. And, if so, your code will not work.

If x is an integer x/100 will return an integer.
And if x is < 100, that integer will be 0. Integer division truncates.
Furthermore there is no integer such that you divide it by 100, you get an answer less than 0.10.
So the conditions of your if statement are never true.

Either declare x to be a float or, failing that, cast x as a float within your if statement.

Assuming either C or C++:
if ( (((float) x)/100) >= 0.00 || (((float x)/100) <= 0.10 )

2006-09-25 09:00:20 · answer #1 · answered by TJ 6 · 0 0

Im not entirely sure what it is that you are trying to count.. that is a bit unclear. If that is C++ then go ahead and change your code to just pointONE++; not the whole equals line because that assignent is taking place BEFORE the increment, or its not assigning your increment at all. Secondly, if that is JAVA or C++, you have an if statement so it only checks once, so the maximium number that pointONE can be is 1. mabey instead you should use a for loop or a while loop or recursive loop? Your question is context based, I must see what is causeing this statement to show up time and time again.

2006-09-25 12:45:13 · answer #2 · answered by Anonymous · 0 0

OK, I see several problems.

1) the variable x needs to be declared as type float. If it is type int, it will not take a fraction, it will only take whole numbers.

2) your if statement is wrong, it should be:
if ((x/100) >= 0.00 && (x/100) <= 0.10) //must use and, not or

3) just pointONE++ will do.
you dont need pointONE = pointONE++

2006-09-25 16:41:39 · answer #3 · answered by justme 7 · 0 0

fedest.com, questions and answers