Re: ubyte in for loops
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 :)
Re: ubyte in for loops
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
Re: ubyte in for loops
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
Re: ubyte in for loops
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@6]U(&UU71E(&EC;W5N="`](#,[(&EC;W5N="`^(#`[("TM:6-O M=6YT*0T*"7L-"@D)=W)I=&5F;&XH(EEO=2!H879E("5S('1R)7,@;&5F="XB M+"!I8V]U;G0L(&EC;W5N="`]/2`Q(#\@(GDB(#H@(FEE6]U(&AA=F4@ M:&%D('1H
Re: ubyte in for loops
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
ubyte in for loops
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 }