<mini rant>This code is "iffy" as it relies on some implementation defined behaviors. Specifically it assumes that outputting a carriage return ('\r') will return the cursor to the beginning of the line without starting a new line. This will not always be the case.
But whatever. I have a million beefs with how C++ is generally taught in schools.</mini rant>
This code moves the letter A across the screen from left to right but I dont understand why |
What's happening this:
1) It's printing a bunch of blanks spaces
2) Then it's printing the 'A' character, surrounded by a blank space on each side of it.
3) Then it returns to the start of the line by outputting a carriage return (so the next character printed will be printed at the start of the line). But again note this is "iffy" behavior.
4) Then it waits a bit (Sleeps for 150 milliseconds)
This is repeated several times. Each time, there is an additional blank space printed during step 1... resulting in the 'A' being printed further to the right.
So logically... if you start with a small number of spaces and keep adding spaces... the A will move to the right (because the additional spaces you print during step 1 "push" it to the right).
Therefore to reverse that and have it move to the left... you'd want to print fewer spaces during step 1.