Left to Right, Right to Left

closed account (N8pE3TCk)
took me a long time to figure out how to make this program going from right to left, to left to right. I now need to figure out how to make it hit the screen edge and start heading back in the next direction and bounce.
please dont just "tell" me the answer but if you can give me some hints id appreciate it.

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

int main()
{
	int pos;
	int spaces;
	pos = 77;//tells program to start at position 77
	while (pos<78)
	{
		spaces = 0;
		while (spaces<pos)
		{
			cout << " ";//displays empty space
			spaces++;
		}
		cout << "A " << "\r";
		pos--;
		Sleep(75);//speeds up the program
	}
	return 0;
}
Hello there again. First, it seems you have an infinite loop there. pos = 77 and your loop will keep going while pos is < 78(it always is).

After you fix that, I guess the easiest way would be create another loop to go the direction you want. 77 to ?, ? to ?? ? I hope I didn't help too much this time.
closed account (N8pE3TCk)
thanks again im sorry i have not been checking the forum over the weekend.
this is what i wrote in class today, its going back and forth forever, which is what i need but its not going to the end of page just back and forth... ?

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
35
36
#include <iostream>
#include <windows.h>
//Taylor Mont
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(75);
        {
            spaces = 0;
            while (spaces<pos)
            {
                cout << " ";
                spaces++;
            }
            cout << "A " << "\r";
            pos--;
            Sleep(75);
        }
    }
    return 0;
}
That's not I had in mind. You could make 2 while statements, one going from, let's say, right to left and another going from left to right. And when you want some code to repeat itself all you have to do is put something around it.

You could even ask the user how many times you want it to loop and use the answer to create the new statement.

1
2
3
4
5
6
7
insert something here
{

while1 ( 78 to 0 )
while2 ( ? to ?? )

}


closed account (N36fSL3A)
This doesn't really help, but I don't know how this:

Sleep(75);//speeds up the program

speeds up the program.
@Taylorjmon

The answer I give here, may help you with your program. The word will go back and forth on the screen, but the program also rotates the word. Just leave that part out if you don't need it. Hope this helped..


http://v2.cplusplus.com/forum/general/111146/
closed account (N8pE3TCk)
thanks everybody, im nt going to ope the link tho @whitenite. I appreciate it but i figured it out with only using one while statement...
it works anyways and ive handed it in.
again everyone thanks for all the tips your wonderfull !
closed account (N8pE3TCk)
@lumpkin , the prof had the sleep set at 300 and it took an eternity for the character to move to one side to the other, when i set it at 75 it moved the character left and right alot faster.
Topic archived. No new replies allowed.