Hi, Im currently implimenting a nested for loop, but there seems to have some known bugs and i dont know whats causing it , how to fix it, etc.
Heres part of my code:

//using some of vector<structs> below:

void Profile()
	int wire = 1;
	int face = 1;
	int i = 0;
	int j = 0;
	int index;
	bool startcheck = true;
	gp_Pnt end; //a sort of struct like, have x, y and z member.
	gp_Pnt2d start; //similar above, but 2 members

	end.SetCoord(0,0,0);//initialize the pnt. ie. set all member values to 0
	//for(int i = 0; i < urange.size();i++)//urange is just a vector struct
	while(i < urange.size())
		if (startcheck == true)//check if is first time entering 
			start.SetCoord( Contour[urange[end.Z()].EdgeStartIndex].u, //save 
			startcheck = false;

		for(int j= 0; j < urange[end.Z()].NbPoints; j++)//the number of time 
		{//this loop loops is varying depending on what is in variable end
			ProfileStore(j, wire, face);//store some information
			if((start.X()==end.X())&&(start.Y()==end.Y()))//test condition
			{//condition:check if two values match.if match, wanna go back to the while loop but with i incremented.
				startcheck == true;
			else if (j == (urange[end.Z()].NbPoints - 1))//the last value
			{//the ProfileFindNextStart(a,b) function will return values that determines how many times the for loop will be looped
				index = urange[end.Z()].EdgeStartIndex + j;
				end = ProfileFindNextStart(Contour[index].u, Contour[index].v);

Im pretty sure i have check the variables are passed correctly,
the problem now is that everything is good when the first time the for loop is entered. but when second time the for loop is entered. THE COUNT OF J IS NOT STARTING FROM ZERO!!! it seems like the j didnt got set.
i.e. for loop looped 4 times when i = 0; gud. nothing wrong with that.
but looped 11-4=7 times when i =1; which is supposed to loop 11 times, but looped 7 times instead and it shows that the count starts from 4 when i was trying to print out to screen.

Anyone know whats causing it? and how to fix it?

VernonDozier commented: Code tags on first post! Thank you! +21

btw, what i intended to do is that when any of the two IF conditions were met, it'll just back to the while loop, but with i incremented by 1.

You have a j variable declared in line 6 and a j variable declared in line 24. These have different scopes. Be very careful with this. You are also passing j to functions outside of what is posted. Check to make sure j isn't passed by reference and that the function isn't changing j. You should either put some debugging statements into your program that displays the value of j (and again, make sure you know WHICH j is you are displaying), or use a debugger and stick some breakpoints. Not having the entire picture, it's hard to speculate what may be causing this. Line 24 will create a NEW j variable and initialize it to 0.

To get a really good idea of what is going on, you must step through the program and display/find out what j is.

Check your line 30, you are comparing or do you want startcheck to become true?

This will check if startcheck equals true:


This will make startcheck become true:


poncho, good catch on the = vs. == .

Regarding the code tags, be careful of \ vs. /. Code tags are these:



as opposed to these:



Also note inline code tags:



You can also highlight text, then press either the green icon on the right for inline code or the # icon (third from right) for regular code tags.

Again, good catch on the = vs. == .

yea sry about the

mess up im a bit drunk atm xD

thanks for replying. well, its my bad, i was trying to fiddle around with different loops before. i was using two for loops then swapped to two while loops then finally one while and one for....Its just different bugs.
what i intended to do is that when either of the two if conditions were met, they should exit the inner for loop and the second if condition will control how many times to loop when the inner for loop is entered next time.
any suggestions how this should be done? thank you

Thank you for spotting that, didnt' notice, it should be a = only, just readjusting the flag. but that doesnt really fix the problem


Impossible to say beyond what I commented last post. What is definitely NOT going on is this:

THE COUNT OF J IS NOT STARTING FROM ZERO!!! it seems like the j didnt got set.

If the inner for loop is being entered on line 24, j is set to 0. Note again that there are two j variables, one defined on line 6, one on line 24. I don't know if you tried my ideas from my earlier post or not, but you haven't commented on them since.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.