hello there! I am new here on Daniweb. I couldn't find an article anywhere on google about moving text in C++.
I have an idea but my problem is, I can't find a way to make my other output still. What I mean is: I don't know how to prevent my other output to be cleared when I use clear screen.

here is my code:

clrscr();

int x;
gotoxy(x,10);
for(x=0; x<=70; x++)
{
         
         gotoxy(x,10);
         cout<<"glennkun";
         clrscr();
         }

any idea guys?

I don't know how to prevent my other output to be cleared when I use clear screen.

That's because clrscr clears the whole screen. :icon_rolleyes: The vanilla console doesn't lend itself well to non-linear drawing. You'd probably be better off either going full GUI, or take advantage of something like curses to get more control.

In case you're curious, here's a short program I wrote using the ncurses library on a linux machine. It's in C, but it's what you're looking for. The ncurses library allows you to place text at different coordinates in a terminal window. In this case, I got the width of the terminal window with getmaxyx() and then in a loop, I cleared the screen, moved to a spot, wrote the text, waited, and then did it again until I got to the end of the screen. Notice that you don't use any standard functions like cout (in C++) or printf (in C). Most of the functions you use to print text and get input are ncurses functions.

Here's my code:

#include <curses.h>
#include <unistd.h> // For sleep()
#include <string.h> // For strlen()

int main(int argc, char* argv[])
{
    char *text = "Hello! How are you today?";
    int text_length;
    int i, max_x, max_y;

    // Get text length
    text_length = strlen(text);

    // Initialize screen for ncurses
    initscr();
    // Don't show cursor
    curs_set(0);
    // Get terminal dimensions
    getmaxyx(stdscr, max_y, max_x);
    // Clear the screen
    clear();

    // Scroll text across the screen once
    for (i = 0; i < (max_x - text_length); i++)
    {
        clear();
        // mvaddstr(y, x, text)
        // mvaddstr moves the cursor to the coordinates given as the first two arguments
        // and then prints the text there
        mvaddstr(0,i,text);
        // refresh should be done after text is changed on the "screen" to actually update the screen
        refresh();
        // Wait 50 ms
        usleep(50000);
    }

    // Scroll text back across the screen
    for (i = (max_x - text_length); i > 0; i--)
    {
        clear();
        mvaddstr(0,i,text);
        refresh();
        usleep(50000);
    }

    // Wait for a keypress before quitting
    getch();

    endwin();

    return 0;
}

I named the file marquee.c and I used this to compile it:

gcc -Wall -o marquee marquee.c -lncurses

Edited 5 Years Ago by Mouche: n/a

This article has been dead for over six months. Start a new discussion instead.