www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - ubyte in for loops

reply DBloke <someone somewhere.com> writes:
I declared a variable cnt of ubyte first as part of the for loop
initialiser to 3, then found it odd that the loop went infinite.

I ran through the debugger and first time through it correctly
decrements value and cnt = 2, the next time round it went to -1, this
did not happen when I declared the cnt to be an uint

I then initialised cnt outside the loop and got the same behaviour

The code was
for(ubyte cnt = 3; cnt > 0; --cnt)
{
    ...
    code here
}
Aug 07 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
DBloke:

 The code was
 for(ubyte cnt = 3; cnt > 0; --cnt)
 {
     ...
     code here
 }
This program: import std.stdio: writeln; void main() { for (ubyte i = 3; i > 0; --i) { writeln(i); } } Prints with dmd 2.047: 3 2 1 It's better to use the D.learn newsgroup for such questions. Bye, bearophile
Aug 07 2010
parent reply DBloke <someone somewhere.org> writes:
Hmmm most strange, using same version of compiler as yours
2.047

I have attached the file for you to try, not sure if it makes
a difference that I am running on 64 bit win7?

I am not after help, just to confirm there maybe a bug with
either ubyte or == ?

Cheers
begin 644 main.d
M:6UP;W)T('-T9"YS=&1I;RP <W1D+F-S=')E86TL('-T9"YC+G1I;64L('-T
M9"YC+G-T9&QI8CL-" T*=F]I9"!M86EN*"D-"GL-" EI;6UU=&%B;&4 =6)Y
M=&4 ;&EM:70 /2`R,#L-" D-" ES<F%N9"AT:6UE*&YU;&PI*3L-" EU:6YT

M:&ES(&ES(&$ 9W5E<W-I;F< 9V%M92(I.PT*"7=R:71E;&XH(DD :&%V92!C
M:&]S96X 82!N=6UB97( 8F5T=V5E;B`Q(&%N9"`R,"(-" D ("` ("` ("( 


M=6YT*0T*"7L-" D)=W)I=&5F;&XH(EEO=2!H879E("5S('1R)7, ;&5F="XB

M92 B96YT97( 82!G=65S<SH (BD[("\O('!R;VUP="!F;W( 82!G=65S<PT*

M"2\O(&-H96-K(&EF(&=U97-S(&-O<G)E8W0-" D):68H9W5E<W, /3T 8VAO





M;VYG+B(L(&=U97-S*3L-" D)?0T*"7T-" EW<FET969L;B B>6]U(&AA=F4 
M:&%D('1H<F5E('1R:65S(&%N9"!F86EL960N(%1H92!N=6UB97( =V%S("5S
<(BP 8VAO<V5N*3L-" ED:6XN9V5T8R I.PT*?0``
`
end
Aug 10 2010
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Your code works, here too:
http://ideone.com/Taq71

So maybe it's the 64 bit system that gives problems. Are you able to minimize
your code, to show just the problem? So it will become a bug report for
bugzilla.

By the way, the D.bugs group is not for general chat, use D.learn instead.

Bye,
bearophile
Aug 10 2010
parent DBloke <someone somewhere.org> writes:
Hi,
The new code you supplied in other post works fine, but the code in the link in
this post does not, after first try, says I have 255 tries and goes infinite on
me, this must be a bug for 64 bit windows? If I cahnge the ubyte to uint works
fine, I guess this must be a 64 bit issue

Thanks for tips :)
Aug 14 2010
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
A different version of your code with some improvements:


import std.stdio, std.cstream, std.random;

void main() {
    uint chosen = uniform(1, 21);

    writeln("This is a guessing game");
    writeln("I have chosen a number between 1 and 20" ~
            " which you must guess");

    int guess = 0;

    foreach_reverse (i; 1 .. 4) {
        writefln("You have %s tr%s left.", i, i == 1 ? "y" : "ies");
        write("enter a guess: "); // prompt for a guess
        scanf("%d", &guess); // Read a guess

        // check if guess correct
        if (guess == chosen) {
            writefln("\nYou guessed it!");
            return;
        }

        // Check for an invalid guess
        if (guess < 1 || guess > 20) // you can add brackets here if you want
            writeln("I said between 1 and 20.");
        else
            writefln("Sorry. %s is wrong.", guess);
    }

    writefln("You have had three tries and failed. The number was %s", chosen);

    din.getc(); // useless?
}

Bye,
bearophile
Aug 10 2010