for loop
Hakate (31)
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
computerquip (1682)
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
Hakate (31)
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
Grey Wolf (2846)
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
Hakate (31)
Oct 28, 2009 at 4:51pm UTC
ok the thing is i think i got an error in my code somewhere since when i try to step backwards the output becomes like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
8 ☼ zÏ?|8 '|ÿÿÿÿoö↕ ☻ ☺ ☼ "☻' |Rr r 8 ♣
Ôø↕ è☼'|☺ ☼ ☺ ♦ s î☺ øø↕ ♠ ☼ Û☺' |E ‼ ♦ Ä
Pö↕ er é?|` '|♠ ☼ 8 8 Ú8Xx♦ ☺ ☼
Hsfr ☼ @♠8 K ° þÿÿÿÚ8Xx ☺ ☼
♠ ☼ 9 8 P8 ¸¢[x☺ ☼ ♠ ä▼% %ú↕ jÔ' |♣
☻ ☺ ☼ öÿÿÿ¨P8 R r ◄ x☺8 ♣ ☼ x☺8 10 ~P8
Sr ? ! ♠ ☼ ÈN8 2 x☺8 ► î☺ ☺ ☼ ♠ º-
☼ 6 →_Xx 8 ☺ ☼ c @ù↕ Rr ↕ ♣"KsPx♣ ☼
☻ ☼ ☺ ¶ù↕ Rrtd ?|.4'|ÿÿÿÿ♣ ☼ ♦ E uÈ↨♂♀ù↕ ë5Tx
ver STK ♠ ☼ ‼ 3 HxdwPx] \s☺ ☺ ☼
çÝr Hx ♠ ☼ D ↕ (èKxÔù↕ é?|☺ ☼ ♦
Kx♣ ☼ 6?Kx4 Hx☺ Æ?Kxd>øØ☺ ☼ ♥ ?|r ?|X☻?
' Ð▼% ☺ ☼ ♣ ☺ Hrter ♥Ô'|☺ ☼ ↑û↕ 5 '|ÉË'|
ù↕ er ⌂À▲% °Ö'|♠ ☼ £Ù'|4 % ðý⌂ ðý⌂ ☺ ☼
Grey Wolf (2846)
Oct 28, 2009 at 4:56pm UTC
Maybe you should post the code that you are having a problem with?
computerquip (1682)
Oct 28, 2009 at 4:57pm UTC
I don't think I could get that output if I tried.
Grey Wolf (2846)
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?
Hakate (31)
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?
Grey Wolf (2846)
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
Mal Reynolds (111)
Oct 29, 2009 at 1:55am UTC
could a ternary statement be what you're looking for?
1 2
bool bVar = false ;
bVar = ( /* some boolean expression */ ? true : false );
computerquip (1682)
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.
firedraco (4744)
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
Grey Wolf (2846)
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
Kiana (186)
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
Topic archived. No new replies allowed.