0

Well, I'm looking to apply some functionality to my code, first i'm trying to make a var go from point x to y and then back from y to x.

int x = 0;

void Bande::printmessage()
    printf("Var %d Xy to Yx: %d\n", x < 10 ? x++ : x-- );
}

int Bande::TestMessage()
{

    for(int i = 0; i <= 10; i++) //loop just to execute the function X times
        Bande::printmessage();

   return 0;
}

Any idea how I can do Increment and decrement and then go back?

that way I tried always returns 10 and 9 when it arrives at 10

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
robert
  • 19
  • 5
  • 1
    The OP recently asked a very similar [question](https://stackoverflow.com/questions/60132997/how-to-make-a-var-go-from-x-to-y-and-then-back-from-y-to-x-and-always-repeat-the) (10K users only) and deleted it. – 1201ProgramAlarm Feb 09 '20 at 17:04
  • 1
    You forgot the Golden Rule Of Computer Programming: "your computer will always do exactly what you tell it to do instead of what you want it to do". You told your computer: if `x` is less than 10, increment it, else decrement it. So if, for example `x` is 8, it will always be incremented. It doesn't matter if now you want `x` to go from 8 to 7, because it's counting down from 10. If `x` is 8, it gets incremented. End Of Story. The End. If you want your computer to do something else, you just need to tell yur computer what your computer should do. Can you try that? – Sam Varshavchik Feb 09 '20 at 17:04
  • 2
    @robert sobrel - PROBLEM: you're asking for *TWO* things: 1) loop through a range (here, either "i=0..10" or "i=10..0") and 2) loop forwards or backwards. The word "And" in your subject line was a giveaway. SOLUTION: 2) Use a boolean [flag](https://stackoverflow.com/questions/17402125/what-is-a-flag-variable), like `isSmallToLarge`, 1) Use the flag to set up your loop. In other words - upvote "ProgrammingFreak" and "accept" his response :) – FoggyDay Feb 09 '20 at 17:31
  • 1
    This would be a good time to learn how to make a simple [state machine](https://en.wikipedia.org/wiki/Finite-state_machine). – ad absurdum Feb 09 '20 at 18:07

1 Answers1

1

The main problem lies in: x < 10 ? x++ : x--. When 9 is incremented to 10, 10-- = 9. Then, when 10 is decremented to 9, 9++ = 10, which means, it will become: 9 10 9 10 9 10 9....

In this case, having the boolean value to dictate this whole process works.

Moreover, printf("Var %d Xy to Yx: %d\n", x < 10 ? x++ : x-- ); gives out a compile error. You have 2 %ds, so you need an extra parameter x.

int x = 0;
bool isSmallToLarge = true;

void xReversePrint() 
{
    if (x <= 0 || x >= 10) 
    {
        isSmallToLarge = !isSmallToLarge;
    }
}

void Bande::printmessage()
{

    printf("Var %d Xy to Yx: %d\n", isSmallToLarge ? x++ : x-- , x );
    xReversePrint();
}


int Bande::TestMessage() 
{

    for(int i = 0; i <= 20; i++) //loop just to execute the function X times
        Bande::printmessage();

   return 0;
}
ProgrammingFreak
  • 498
  • 3
  • 13