Page 1 of 1

how to stop from looping

Posted: Wed May 19, 2010 9:24 pm
by pythip
I call a function which looks like this, and it keeps looping by resetting itself, this code is also in a function, which has a for loop that sets flip, and once the for loop is done calls checkFlip(), and then does another for loop and checkFlip(). The problem with this code it it sets flip to false and then comes back around somehow and sees that its true

Code: Select all

void checkFlip()
{
	if (flip == false)
	{
		for (int i = 1; i<=7; i+=1)
		{
			flip();
		}
	}
	else if (flip == true)
	{
	flip = false;
	}
}

Re: how to stop from looping

Posted: Wed May 19, 2010 9:28 pm
by Bakkon
We need to see more code. What scope is 'flip' in?

Re: how to stop from looping

Posted: Wed May 19, 2010 10:05 pm
by X Abstract X
You didn't post enough code for us to determine the problem. Anyway, I felt the need to simplify the code you did post in an attempt to help. Don't take this the wrong way but your code was a bit overly complicated.

Code: Select all

void checkFlip() {
    if (flip) {
        flip = false;
    } else {
        for (int i = 0; i < 7; i++)
            flip();
    }
}

Re: how to stop from looping

Posted: Wed May 19, 2010 10:35 pm
by avansc
X Abstract X wrote:You didn't post enough code for us to determine the problem. Anyway, I felt the need to simplify the code you did post in an attempt to help. Don't take this the wrong way but your code was a bit overly complicated.

Code: Select all

void checkFlip() {
    if (flip) {
        flip = false;
    } else {
        for (int i = 0; i < 7; i++)
            flip();
    }
}
you didnt change the code really you just used a else instead of a else if, its no more less complicated, if anything his is easier to read.

Re: how to stop from looping

Posted: Wed May 19, 2010 10:52 pm
by X Abstract X
avansc wrote:
X Abstract X wrote:You didn't post enough code for us to determine the problem. Anyway, I felt the need to simplify the code you did post in an attempt to help. Don't take this the wrong way but your code was a bit overly complicated.

Code: Select all

void checkFlip() {
    if (flip) {
        flip = false;
    } else {
        for (int i = 0; i < 7; i++)
            flip();
    }
}
you didnt change the code really you just used a else instead of a else if, its no more less complicated, if anything his is easier to read.

I'd say his loop is damn hard to read. You could argue that the rest is personal preference but it's bloated in my opinion. Forums are a place for everyone to pitch in opinions and people can take from it what they want, I'm not claiming my preference is gold.

Re: how to stop from looping

Posted: Thu May 20, 2010 4:40 am
by pritam
Just from a quick look at your code.

Code: Select all

void checkFlip()
{
   if (flip == false)
   {
      for (int i = 1; i<=7; i+=1)
      {
         flip();
      }
   }
   else if (flip == true)
   {
   flip = false;
   }
}
I think you could go about it like this as well.

Code: Select all

void checkFlip() {
   if (flip == false) {
      for (int i = 1; i<=7; i+=1) {
         flip();
      }
   }
   flip = false;
}
I may be wrong, correct me if I am.

Re: how to stop from looping

Posted: Thu May 20, 2010 6:59 am
by Amarant
Is 'flip' supposed to be a function or a variable?

Code: Select all

flip = false; // flip is a variable ?
flip(); // or is it a function...

Re: how to stop from looping

Posted: Thu May 20, 2010 8:18 am
by RyanPridgeon
Amarant wrote:Is 'flip' supposed to be a function or a variable?

Code: Select all

flip = false; // flip is a variable ?
flip(); // or is it a function...
That's what I thought, I'm amazed that even compiled!

Re: how to stop from looping

Posted: Thu May 20, 2010 10:04 am
by Ginto8
Amarant wrote:Is 'flip' supposed to be a function or a variable?

Code: Select all

flip = false; // flip is a variable ?
flip(); // or is it a function...
The only reason it works is because C++ defines functions not just by their name but also their parameters. func(void) (a function, also written as func())is different from just plain func (the variable).

Anyway, looking at the original problem, you're using mutual recursion without a condition to stop it. We'll need to see the code for flip() as well as checkFlip().

Re: how to stop from looping

Posted: Thu May 20, 2010 2:21 pm
by xiphirx
You need to provide more code. We need to see where "flip" is modified to true, if it ever is.


Taking a look at the code, flip is quite useless. When you call "checkFlip()" you first see if flip is false, lets just say it is, so the code will start the loop, which will halt your game execution until it is done. I am assuming flip() sets flip to false at the end? So, the sequences goes like this
check flip if false
flip is false
repeat flip() seven times
flip is still false
if flip is true (which it seems it cant be)
set flip to false

Right now, it looks like a function that just starts another function... flip is probably only set to true in your flip() function.

I think what you want, is a counting sequence that will execute along with your overall loop.

Re: how to stop from looping

Posted: Fri Oct 22, 2010 4:49 am
by MacJordan
pythip wrote:I call a function which looks like this, and it keeps looping by resetting itself, this code is also in a function, which has a for loop that sets flip, and once the for loop is done calls checkFlip(), and then does another for loop and checkFlip(). The problem with this code it it sets flip to false and then comes back around somehow and sees that its true

Code: Select all

void checkFlip()
{
	if (flip == false)
	{
		for (int i = 1; i<=7; i+=1)
		{
			flip();
		}
	}
	else if (flip == true)
	{
	flip = false;
	}
}

if you want to stop the looping you can use the break; statements;
When break statement is call cursor position transfer outside the for loop.
try this statement to stop your for loop.

Re: how to stop from looping

Posted: Fri Oct 22, 2010 12:38 pm
by dandymcgee
MacJordan wrote: if you want to stop the looping you can use the break; statements;
When break statement is call cursor position transfer outside the for loop.
try this statement to stop your for loop.
Appreciate you trying to help, but please read the last post date before posting. This thread is over 5 months old.