• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# This makes no sence at all

## Recommended Posts

Hi people I have a probelm with my C program. I am a reasonably experienced programmer, but i can say i have no expanation for this. I made the loops iterate once as I was trying to workl out what the hell was happening, and came across the bizzareness. The program is the beginnings of a terrain system. This is the basic layout of my problem: The is more code in beteen, but this is the jist.
int x, z, count = 0;
for (z = 0; z < 1; z++)    //One loopz

{
for (x = 0; x < 1; x++)
{
//Following code I added to test this phenomenon

if (z == 0)
count++:
if (z == 1)
count++;
if (x == 0)
count++;
if (x == 1)
count++;
if (x == 2)
count++;
if (x == 3)
count++;
}
}

Count was added to see what the hell was going on. As it turns out count finished with the value of 5...thats impossible given that X = 0 and Z = 0 the whole time, count can only be 2!! There is nothting to change the value of X and Z other than the loops which only loop 1 time. What the hell is going on here? [edited by - Mr Lane on March 20, 2004 12:50:11 AM]

##### Share on other sites
Count ends up 2 for me, however, your code has a colon instead of a semicolon after the first "count++"...MSVC++ threw a fit about it, but your compiler might not be.

##### Share on other sites
The code i posted isnt the exact code, its just a example of what is happening, so the colon is not the problem. This is the full code:

void createTerrain(terrainList_t *terrainList){	int x,z, bFlip, nextX, i, n;	int count;	char ch;	static terrainNode_t newNode;	terrainTriangle_t triangle;	FILE *outFile;	outFile = fopen ("Out.txt", "w");	//Create attributes for the terrain	newNode.terrain.lengthX = 2;		newNode.terrain.lengthZ = 2;	newNode.terrain.numTriangles = 2*2*2;	newNode.terrain.origin.x = 0.0;			newNode.terrain.origin.y = 0.0;	newNode.terrain.origin.z = 0.0; 	for (z = 0; z < 1; z++)	{		bFlip = 0;		i=0;		nextX = 0;		for (x = 0; x < 1; x++)		{			//Triangles are ordered across X, then Z                        //Y values are all hardcoded to 0.5			triangle.Vertex[0].x = 0.0+nextX;			triangle.Vertex[0].z = 0.0+z;			triangle.Vertex[0].y = 0.5;   	   		if (bFlip == 0)			{		       			triangle.Vertex[1].x = 0.0+nextX;			    triangle.Vertex[1].z = 1.0+z;				triangle.Vertex[1].y = 0.5;				bFlip = 1;			}			else			{				triangle.Vertex[1].x = .0+nextX;				triangle.Vertex[1].z = 0.0+z;				triangle.Vertex[1].y = 0.5;				bFlip = 0;			}				    		triangle.Vertex[2].x = 1.0+nextX;			triangle.Vertex[2].z = 1.0+z;			triangle.Vertex[2].y = 0.5;			i++;			if (i == 2)			{				i = 0;				nextX++;			}							newNode.terrain.Triangles[z][x] = triangle;			   			//Test what the hell is happening here.   			count = 0; 			if (z == 0)			    count++;			if (z == 1);			    count++;			if (x == 0)                            count++;         		if (x == 1);                     	    count++;			if (x == 2)        		    count++;			if (x == 3);			    count++;			if (z == 100);			    count++;         	}        }    			        newNode.nextNode = NULL;		if (terrainList->start == NULL)		{		terrainList->start = &newNode;		terrainList->end = &newNode;        }	else	{ 		terrainList->end->nextNode = &newNode; 		terrainList->end = &newNode;	}	return;}

Even in this code, there is no way z could also == 100, but the execution goes through the if statment for z == 0, z == 1 and z == 100.

[edited by - Mr Lane on March 21, 2004 1:06:57 AM]

##### Share on other sites
You have semicolons after your if statements.