| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
August 07, 2010 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
}
| ||||
August 07, 2010 Re: ubyte in for loops | ||||
|---|---|---|---|---|
| ||||
Posted in reply to DBloke | 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
| |||
August 10, 2010 Re: ubyte in for loops | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile Attachments: | 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 | |||
August 10, 2010 Re: ubyte in for loops | ||||
|---|---|---|---|---|
| ||||
Posted in reply to DBloke | 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 | |||
August 10, 2010 Re: ubyte in for loops | ||||
|---|---|---|---|---|
| ||||
Posted in reply to DBloke | 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
| |||
August 14, 2010 Re: ubyte in for loops | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 :) | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply