moving text characters in the console window?

hi does anybody know the code to move text characters in the console window?
or maybe a link? i couldnt find any but i think it might have some thing to
do with x and y.
You can use the curses library
http://pdcurses.sourceforge.net/ curses for Windows
http://www.gnu.org/software/ncurses/ curses for non-Windows
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ tutorials on how to use them
Windows console (as in windows ONLY, I don't recall the unix alternative but I'm sure there is one) also has an option like that. (And of course it has to do with x and y, how else would you define the coords?)

I recall reading this from somewhere else in the forums so I take no credit for the code that follows. I believe it is Duoas's...
But yes curses is the *real* way to do it. Curses is meant for console manipulation so it is more suited, but this is from windows console libraries and it's rather useful unto itself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <windows.h>

// Give this a row and column in integers and it will move the cursor.

void curPos(int x, int y)
{
	HANDLE hStdout;
	CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
	hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
	GetConsoleScreenBufferInfo(hStdout, &csbiInfo);
	csbiInfo.dwCursorPosition.X=x;
	csbiInfo.dwCursorPosition.Y=y;
	SetConsoleCursorPosition(hStdout, csbiInfo.dwCursorPosition);
}
i found a way to do it. bit i want to make it more simple and efficient because i want to make it much bigger. here is the code maybe you will have a better idea. it runs on an infinite loop until the user types exit. i converted the string to an integer then used a switch/case followed by a system("CLS").
i had to use two posts to fit it all. you might have to compile and run it to understand how it works.

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string mp1 = "|";
    string mp2 = "-";
    string mp3 = "-";
    string mp4 = "|";
    string mp5 = "|";
    string mp6 = "#";
    string mp7 = " ";
    string mp8 = "|";
    string mp9 = "|";
    string mp10 = " ";
    string mp11 = " ";
    string mp12 = "|";
    string mp13 = "|";
    string mp14 = "-";
    string mp15 = "-";
    string mp16 = "|";
    string edge1 = "*";
    string edge2 = "*";
    string edge3 = "*";
    string edge4 = "*";
    string edge5 = "*";
    string edge6 = "*";
    string edge7 = "*";
    string edge8 = "*";
    string STRmove;
    int INTmove;
    int counter = 10;
    int numMP6 = 1;
    int numMP7 = 2;
    int numMP10 = 3;
    int numMP11 = 4;
Last edited on
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
 while ( counter > 0)
        {
            cout  << mp1 << mp2 << mp3 << mp4 << endl;
            cout << mp5 << mp6 << mp7 << mp8 << endl;
            cout << mp9 << mp10 << mp11 << mp12 << endl;
            cout << mp13 << mp14 << mp15 << mp16 << endl;

            cout << "enter to move up, down, left, right, or exit: ";
            cin >> STRmove;

                    if ( STRmove == "exit")
                        {
                            INTmove = 0;
                        }

                          if (STRmove == "down")
                        {
                            INTmove = 3;
                        }

                          if ( STRmove == "left")
                        {
                            INTmove = 4;
                        }

                          if ( STRmove == "up")
                        {
                            INTmove = 1;
                        }

                          if ( STRmove == "right")
                        {
                            INTmove = 2;
                        }

                                switch(INTmove)
                                {
                                   case 0:

                                        cout << "exiting game" << endl;
                                        return 0;
                                        break;

                                    case 1:                             //up

                                    if(mp10 == "#")
                                    {
                                                mp6 = "#";
                                                mp7 = " ";
                                                mp10 = " ";
                                                mp11 = " ";
                                                 system("CLS");
                                                break;
                                    }

                                     if(mp11 == "#")
                                    {
                                                mp6 = " ";
                                                mp7 = "#";
                                                mp10 = " ";
                                                mp11 = " ";
                                                 system("CLS");
                                                break;
                                    }


                                    else
                                                mp6 = mp6;
                                                mp7 = mp7;
                                                mp10 = mp10;
                                                mp11 = mp11;
                                                system("CLS");
                                                break;

                                    case 2:                              //right

                                    if(mp6 == "#")
                                    {
                                                mp6 = " ";
                                                mp7 = "#";
                                                mp10 = " ";
                                                mp11 = " ";
                                                 system("CLS");
                                                break;
                                    }

                                       if(mp10 == "#")
                                    {
                                                mp6 = " ";
                                                mp7 = " ";
                                                mp10 = " ";
                                                mp11 = "#";
                                                 system("CLS");
                                                break;
                                    }

                                    else
                                         mp6 = mp6;
                                                mp7 = mp7;
                                                mp10 = mp10;
                                                mp11 = mp11;
                                                system("CLS");
                                                break;

                                            case 3:    //down

                                          if(mp6 == "#")
                                          {
                                                mp6 = " ";
                                                mp7 = " ";
                                                mp10 = "#";
                                                mp11 = " ";
                                                system("CLS");
                                                break;
                                          }

                                              if(mp7 == "#")
                                          {
                                                mp6 = " ";
                                                mp7 = " ";
                                                mp10 = " ";
                                                mp11 = "#";
                                                system("CLS");
                                                break;
                                          }


                                    else
                                               mp6 = mp6;
                                                mp7 = mp7;
                                                mp10 = mp10;
                                                mp11 = mp11;
                                                system("CLS");
                                                break;


                                    case 4:  //left

                                    if(mp7 == "#")
                                    {
                                                mp6 = "#";
                                                mp7 = " ";
                                                mp10 = " ";
                                                mp11 = " ";
                                                system("CLS");
                                                break;
                                    }

                                       if(mp11 == "#")
                                    {
                                                mp6 = " ";
                                                mp7 = " ";
                                                mp10 = " ";
                                                mp11 = "#";
                                                system("CLS");
                                                break;

                                   }

                                    else
                                                mp6 = mp6;
                                                mp7 = mp7;
                                                mp10 = mp10;
                                                mp11 = mp11;
                                                system("CLS");
                                                break;

                                   default:
                                                mp6 = mp6;
                                                mp7 = mp7;
                                                mp10 = mp10;
                                                mp11 = mp11;
                                                system("CLS");
                                                break;
                                }
        }
}
Last edited on
Why do not you use arrays for homogeneous variables?
string mp[16] = {"|", "-", "-", "|", ....};

Why do you use strings for string of one character? Maybe better to use char?
char mp[16] = {'|', '-', '-', '|', ....};
because i couldn't get it to work earlier. i was done by the time i figured it out.
U'd be able to rewrite this code:
1
2
3
4
            cout  << mp1 << mp2 << mp3 << mp4 << endl;
            cout << mp5 << mp6 << mp7 << mp8 << endl;
            cout << mp9 << mp10 << mp11 << mp12 << endl;
            cout << mp13 << mp14 << mp15 << mp16 << endl;


to
1
2
3
4
5
for(int i=0; i<15; ++i)
{
    cout << mp[i];
    if((i+1)%4 == 0) cout << endl;
}


This is not shorter if to count in lines, but is shorter in symbols and much more shorter in used operators, and it is clearer and more universal. What if you need 256 variables instead of 16?
Topic archived. No new replies allowed.