moving words

Hello. Here is my code so far:
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
37
38
39
40
41
42
43
// Rotate.cpp : main project file.

#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

void gotoXY(int x, int y);

int main()
{
    string rotating_string = "moving...";
	int len = rotating_string.length();
	char letter_holder;

	do
	{
		gotoXY(14,13);
		cout << rotating_string;
		Sleep(200);
		letter_holder = rotating_string[len];
		for( int x=len;x>0;x--)
		{
			rotating_string[x] = rotating_string[x-1];
		}
		rotating_string[0] = letter_holder;

	} while(true);


    return 0;
}

void gotoXY(int x, int y)
{
	CursorPosition.X = x;
	CursorPosition.Y = y;
	SetConsoleCursorPosition(console,CursorPosition);
}

I want the text to keep moving across the screen without disappearing at a certain point. How would I do this?
Last edited on
@megasnorlax

Is this what you were thinking of? I changed the direction of the rotating word to the way you would probably read it, right to left rotation, and the word scrolls from the left of the screen, to the right, then back to left, etc, until you quit the program.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Rotate.cpp : main project file.

#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

void gotoXY(int x, int y);

int main()
{
	string rotating_string =  "moving...";
	int len = rotating_string.length();
	char letter_holder;
	do
	{
		for (int m=4;m<70;m++)
		{
			for (int o=0;o<len;o++)
			{
				gotoXY(m,13);
				cout << rotating_string << " ";
				Sleep(200);
				letter_holder = rotating_string[0];
				for( int x=0;x<len;x++)
				{
					rotating_string[x] = rotating_string[x+1];
				}
				rotating_string[len-1] = letter_holder;

			}
		}
		for (int m=70;m>3;m--)
		{
			for (int o=0;o<len;o++)
			{
				gotoXY(m,13);
				cout << rotating_string << " ";
				Sleep(200);
				letter_holder = rotating_string[0];
				for( int x=0;x<len;x++)
				{
					rotating_string[x] = rotating_string[x+1];
				}
				rotating_string[len-1] = letter_holder;

			}
		}
	} while(true);
	gotoXY(25,24);
	return 0;
}

void gotoXY(int x, int y)
{
	CursorPosition.X = x;
	CursorPosition.Y = y;
	SetConsoleCursorPosition(console,CursorPosition);
}


EDIT: If you're wanting to use a longer string for the moving marque, just change the for loops to..

1
2
3
4
5
6
for (int m=4;m<80-(len+1);m++)  // Left to right on screen
and
for (int m=80-(len+1);m>3;m--) // Right to left
// This way, the end of the string will stop at the edge of the screen and then start reversing direction
// whithout messing up the rest of the screen layout. And change the 80, to however far distance you
// want the text to scroll up to.. 
Last edited on
Sadly, no this is not what I meant. I should have been more specific. I want the words "moving..." to start at the right of the display screen and slide all the way to the left without cutting off and starting again.
@megasnorlax

Then try removing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (int m=4;m<70;m++)
		{
			for (int o=0;o<len;o++)
			{
				gotoXY(m,13);
				cout << rotating_string << " ";
				Sleep(200);
				letter_holder = rotating_string[0];
				for( int x=0;x<len;x++)
				{
					rotating_string[x] = rotating_string[x+1];
				}
				rotating_string[len-1] = letter_holder;

			}
		}

from the above code. That part was to move the text from left to right. Then all you have left would be right to left. After it finishes, it'll start back on the right side again, moving left.
To rotate the string, use std::rotate()
http://en.cppreference.com/w/cpp/algorithm/rotate

For instance (this just rotates the two strings and then displays them in the same place):

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
// Rotate.cpp : main project file.

#include <iostream>
#include <string>
#include <Windows.h>
#include <algorithm>

void gotoXY( SHORT x, SHORT y ) ;

int main()
{
    std::string rotate_right = "rotating right..." ;
    std::string rotate_left = "rotating left..." ;

    do
    {
        gotoXY(14,13);
        std::cout << rotate_right << '|' << rotate_left ;
        Sleep(200);
        std::rotate( rotate_right.begin(), rotate_right.end()-1, rotate_right.end() ) ;
        std::rotate( rotate_left.begin(), rotate_left.begin()+1, rotate_left.end() ) ;
    }
    while(true);
}

void gotoXY( SHORT x, SHORT y )
{
    static const HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);

    COORD CursorPosition = { x, y } ;
    SetConsoleCursorPosition(console,CursorPosition);
}
Topic archived. No new replies allowed.