g++ problem!?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
haaaz
New poster
Posts: 29
Joined: Sun Sep 08, 2002 8:02 am

g++ problem!?

Post by haaaz » Wed Oct 15, 2003 2:05 pm

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?

haaaz
New poster
Posts: 29
Joined: Sun Sep 08, 2002 8:02 am

Post by haaaz » Wed Oct 15, 2003 2:31 pm

I forget to clarify a point: I'm using gcc version 3.3.1 (cygming special)

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Thu Oct 16, 2003 2:37 pm

int last_two_nonzero_digits(unsigned int n)
{
while(n%10==0)n=n/10;
return n%100;
}

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Thu Oct 16, 2003 2:44 pm

Ups, sorry, it's not what I wanted to write. What output do you want do get for n=10?

haaaz
New poster
Posts: 29
Joined: Sun Sep 08, 2002 8:02 am

Post by haaaz » Fri Oct 17, 2003 4:30 pm

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

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Thu Oct 23, 2003 12:43 pm

Hmm - strange. On my g++ 3.3 it works just fine.

Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:

Post by Julien Cornebise » Thu Oct 30, 2003 12:06 pm

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)

Post Reply

Return to “C++”