solved  for loop

Hakate (29)   Link to this post
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 (871)   Link to this post
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
Hakate (29)   Link to this post
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
Grey Wolf (1599)   Link to this post
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
Hakate (29)   Link to this post
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 (1599)   Link to this post
Maybe you should post the code that you are having a problem with?
computerquip (871)   Link to this post
I don't think I could get that output if I tried.
Grey Wolf (1599)   Link to this post
If, as you have previously said, you are outputting from an array, have you got guards to stop it going out of bounds?
Hakate (29)   Link to this post
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 (1599)   Link to this post
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
Mal Reynolds (111)   Link to this post
could a ternary statement be what you're looking for?
1
2
bool bVar = false;
bVar = ( /* some boolean expression */ ? true : false );
computerquip (871)   Link to this post
This is ridiculous. If we can't figure out what he wants, there's no point in wasting our wisdom on guessing.
firedraco (2606)   Link to this post
ZOMG! Gret Wolf has 1337 posts!!!

Grey Wolf (1337) Oct 28, 2009 at 4:16pm
Last edited on
Grey Wolf (1599)   Link to this post
ZOMG! Gret Wolf has 1337 posts!!!

???

Edit:
sorry just noticed. :0)
Last edited on
Kiana (72)   Link to this post
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
high5nothi5 (4)   Link to this post
looks good to me..

This topic is archived - New replies not allowed.