Page 1 of 1

g++ problem!?

Posted: Wed Oct 15, 2003 2:05 pm
by haaaz
I want to extract the last two non-zero digits from an int n, using the following code:

[cpp]while (n%10 == 0)
n /= 10;
n %= 100;[/cpp]

if i input 26300, the change of value of n is as expected
n=26300
n=2630
n=263
n=63

but it doesn't run as expected when the value of n is 10...
[cpp]n%10 == 0 [/cpp]-> ture
[cpp]n /= 10 [/cpp]-> n=1
[cpp]n%10 == 0 [/cpp]-> what will be executed : 0%10 == 0 !!!!!! but I expect 1%10 == 0
what actually is the radical of this problem?

Posted: Wed Oct 15, 2003 2:31 pm
by haaaz
I forget to clarify a point: I'm using gcc version 3.3.1 (cygming special)

Posted: Thu Oct 16, 2003 2:37 pm
by Krzysztof Duleba
int last_two_nonzero_digits(unsigned int n)
{
while(n%10==0)n=n/10;
return n%100;
}

Posted: Thu Oct 16, 2003 2:44 pm
by Krzysztof Duleba
Ups, sorry, it's not what I wanted to write. What output do you want do get for n=10?

Posted: Fri Oct 17, 2003 4:30 pm
by haaaz
i got into a infinite loop because the statement n%10==0 always evaluates to TRUE. I can't figure out why the value of n can become zero if i enter n as 10, 20 or even 100

Posted: Thu Oct 23, 2003 12:43 pm
by Krzysztof Duleba
Hmm - strange. On my g++ 3.3 it works just fine.

Posted: Thu Oct 30, 2003 12:06 pm
by Julien Cornebise
So does it on my
g++ version 3.2 (mingw special 20020817-1)
(mingw is also a gnu windows environment, such as cygwin, but far lighter, because far less-featured)