Oct 28, 2009 at 3:41pm UTC
Hello
i need to make a for loop that changes direction everytime a specific number is randomized, im not really familiar with that.
example: if number 4 is randomized the loop changes from ++ to -- also the values changes and if another 4 is the value the loop changes from -- to ++
any help appreciated
Oct 28, 2009 at 4:14pm UTC
I of course don't understand your objective. Without it I can't make an accurate method of accomplishing what your wanting.
However, I'm great at guessing. I'm assuming that you want something to lower or heighten the value of a variable depending if it's lower or higher than a constant value. This can be accomplished using if / else statements:
1 2
if (myNumber > 10) std::cout << "myNumber is greater than 10!" << std::endl;
else if (myNumber >=0) std::cout << "myNumber is less than 10! NOOOOO" << std::endl;
Last edited on Oct 28, 2009 at 4:15pm UTC
Oct 28, 2009 at 4:38pm UTC
No not really, i loop out something in a array in a order i type in like 0 to 5 but if i type in the number 4 i want it to loop from 4 to 0 aka backwards lets say i loop out 0 to 5
0 1 2 3 4 5
and when i type in 4 it shuld be like this
4 3 2 1 0
also i want it to do that in the same loop as the other
Last edited on Oct 28, 2009 at 4:40pm UTC
Oct 28, 2009 at 4:39pm UTC
Like computerquip, I don't fully understand what you are trying to do
perhaps somthing along the lines of:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
using namespace std;
int main()
{
int step = 1;
for (int i = 1; i > 0; i += step)
{
cout << i << endl;
if (i == 5) step = -1;
}
return 0;
}
Edit:
from the above post, It looks like you want two loops the first to display, then get some input, then another to display the reverse.
Last edited on Oct 28, 2009 at 4:48pm UTC
Oct 28, 2009 at 4:56pm UTC
Maybe you should post the code that you are having a problem with?
Oct 28, 2009 at 4:57pm UTC
I don't think I could get that output if I tried.
Oct 28, 2009 at 6:26pm UTC
If, as you have previously said, you are outputting from an array, have you got guards to stop it going out of bounds?
Oct 28, 2009 at 9:47pm UTC
That was the problem actually i forgot it starts on 0 :/
is it possible to change a boolean from true to false and false to true in 1 if statement btw?
Oct 28, 2009 at 11:16pm UTC
is it possible to change a boolean from true to false and false to true in 1 if statement btw?
Again I'm not entirely sure what you want, possibly:
1 2 3
bool test = true ;
//...
test = !test; // set test to its opposite value
Last edited on Oct 28, 2009 at 11:18pm UTC
Oct 29, 2009 at 3:53am UTC
This is ridiculous. If we can't figure out what he wants, there's no point in wasting our wisdom on guessing.
Oct 29, 2009 at 3:57am UTC
ZOMG! Gret Wolf has 1337 posts!!!
Grey Wolf (1337) Oct 28, 2009 at 4:16pm
Last edited on Oct 29, 2009 at 3:57am UTC
Oct 29, 2009 at 8:50am UTC
ZOMG! Gret Wolf has 1337 posts!!!
???
Edit:
sorry just noticed. :0)
Last edited on Oct 29, 2009 at 8:51am UTC
Nov 3, 2009 at 1:52am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <winuser.h>
// ...
BOOL bDirection = TRUE;
int i = 0;
for (; /* set condition here*/ ; (bDirection ? i++ : i--))
{
// do stuff
if (IsFour())
bDirection = TRUE;
if (IsTen())
bDirection = FALSE;
}
// if bDirection == TRUE, then i++
// if bDirection == FALSE, then i--
?
Last edited on Nov 3, 2009 at 1:53am UTC