Question , Left to Right

closed account (N8pE3TCk)
This code moves the letter A across the screen from left to right but I dont understand why, I want it to go from right to left but this is for homework so I dont want the outright answer, but what in this code makes the letter a go left to right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
	int pos;
	int spaces;
	pos = 1;
	while (pos<78)
	{
		spaces = 1;
		while (spaces<pos)
		{
			cout << " ";
			spaces++;
		}
		cout << " A " << "\r";
		pos++;
		Sleep(150);
	}
	return 0;
}
Think -- instead of ++. You'll have to change one or two things in your code but I'm sure you'll figure out.
closed account (N8pE3TCk)
I had thought of that, im trying that now but it dosnt seem to be working currently for me. I appeiciate that your trying to be vague tho and not give me the straight answer , thank you.
<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.
closed account (3qX21hU5)
First I would like to say I congratulate you on wanting to do this by yourself and not be handed the answer. It's refreshing to see someone that wants to do their own work on here instead of people asking us to do it for them like we are employees or something.

Anyways for your problem you might want to reconsider your structure of the program and redo it from scratch (If your professor doesn't mind).

Here is what you might need

1) Use a std::string to hold a string full of spaces (The length of the console).

2) A way to clear the console screen. Now you probably don't want to use system() anything so I would recommend using this cout << std::string(100, '\n');.

3) One while loop to hold everything in.

Now here is a basic layout you can use.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main()
{
    // Declare your string to hold the spaces here. Make sure it is the length
    // of the console - 1 (So you can put a character at the end that will move).
    string spaces(78, ' ');
    
    // This is our while loop. We will run it while our string of spaces is not empty.
    while (!spaces.empty())
    {
        // The first thing in the loop is to clear the console.

        
        // Next you are going to want to print the spaces.

        
        // After you have printed the spaces you are going to want to print the character
        // you want to be on the end of the spaces.

        
        // After you did that you are going to want to take one space off of the end of the string.
        // Hint: There is a method of std::string that will help with this.

        
        // Now we sleep for a bit so it doesn't go to fast.

    }
    return 0;
}


Hope this helps a bit and remember there is always other ways to do this so feel free to come up with your own solution to this problem also.
Last edited on
closed account (N8pE3TCk)
thank you everyone for the help, @Disch, thank you for explaining exactly why its moving left to right. @Zereo, our prof has yet to teach us "#include <string> " , so i don't think i should venture down that avenue.
I have yet to get this to work on my own , but every time the prof supplies us with sample code the modification to it is usually really minor... its just something im not seeing....
thanks again everyone.
closed account (3qX21hU5)
No problem sometimes it is best to stay with what the professor has taught you already specially since some of them tend to take points off for using stuff they haven't taught you yet (Which I still don't understand that ;p).

But since you aren't going to be using a solution with string's I'll post this just incase it might help others in the future or it might help give you some ideas.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <windows.h>

int main()
{
    // Create a string with 78 spaces and a X on the end
    std::string spaces(78, ' ');
    spaces.push_back('X');

    // Run the loop until it returns empty which case it is time to exit the program.
    while (!spaces.empty())
    {
        // Clear the console then print the string we created above.
        std::cout << std::string(100, '\n') << spaces;
        
        // Erase the first character in our string so it give the impression it is moving left.
        spaces.erase(spaces.begin());
        
        // Sleep so it doesn't go by to fast.
        Sleep(150);
    }
    return 0;
}


Anyways best of luck with your assignment and if you have any other questions just feel free to ask and we would be glad to help answer any questions you might have.
Last edited on
closed account (N8pE3TCk)
thanks again , and I believe he would take points away if i were to use functions that he has yet to teach. They would look at is as cheating for sure
closed account (N8pE3TCk)
ya... i have no idea lol
this is whats asked of me...
Modify the original program to start the animation from the right side moving to the left side. (HINT: Start with the original cout << "A" << "\r" and when you have problems ... and you will... modify the output to be
cout << " A " << "\r" )... that is the letter 'A' with spaces on both sides.

In your documented code, describe the reason that the change fixes the multiple A problem.
Try working out with the pos variable. It's going from 1 to 78, right? And you want to...
closed account (N8pE3TCk)
omg, i feel silly now lol finally finally got it working. thanks everyone , esp. locien.
i have a few bugs that I still need to work out but it is doing what i need it to do now.
Topic archived. No new replies allowed.