Foreach loop behaviour and manipulation

Binarydepth binarydepth at gmail.com
Thu Nov 28 15:36:18 PST 2013


Hi guys I'm having some problems. Calculations are not working as 
expected and I get segmentation fault. I used the 2.059 version 
and it runs after compilation on compileonline.com

But I imagine is either a rookie mistake or a bug, what I'm 
curious about is the foreach loop.

I'm wondering in the case of manipulating the variable from the 
foreach loop, Do I have to reset that variable so the loop can 
work as intended ?(chronologically).

If you don't get what I'm saying check for the comment on the 
code below.

Code :
------------------------------------------------------------------------
import std.stdio : write, readf;
void main()
{
int a, r, f, temp;
int[102] arr;
write("Digite su año de nacimiento : ");
readf(" %d", &a);
write("\n");
foreach(t; 1..51)
     {
	temp=t;
	t*=20;
	t+=420;
	t*=5;
	t+=3;
     arr[temp-1]=t-a;
	t=temp;
	t*=5;
	t+=50;
	t*=20;
	t+=1013;
     arr[temp]=t-a;
     t=temp;//Do I need this for the foreach loop to work as 
intended ?
	}
write("BD\tAnonimo\n");
foreach(count; 0..102)
     {
         write(arr[count]);
         if(count%2==0)
         write("\n");
         else
         write(" : ");
     }
}
-------------------------------------------------------------------------------


More information about the Digitalmars-d-learn mailing list