C++ standard library headers help

i am trying to make this code work using only c++ standard library headers (not using conio.h) and allow the character on the map to move without pressing enter after each response, any help would be great.

Original Code
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
#include <iostream>
#include <conio.h>
using namespace std;
char map[15][21] = {

"+------------------+",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"+------------------+"};
int main() {
char kbinput;
int x=3;
int y=3;
int col, row;
while (true) {
system("cls");
for(row = 0; row <= 16; row++) {
for (col = 0; col <= 20; col++)
cout << map[row][col];
cout << endl;
}
cout << "Enter WASD to go up and down left and right." << endl;
kbinput = _getch();
switch(kbinput) {
case 'd':
if(map[y][x+1]== '|') break;
map[y][x]=' ';
x += 1;
map[y][x] = '+';
break;
case 'a':
if(map[y][x-1]== '|') break;
map[y][x]=' ';
x -= 1;
map[y][x] = '+';
break;
case 'w':
if(map[y-1][x]== '-') break;
map[y][x]=' ';
y -= 1;
map[y][x]='+';
break;
case 's':
if(map[y+1][x]== '-') break;
map[y][x]=' ';
y += 1;
map[y][x] = '+';
break;
}
}
}


Modefied Code (but user has to use enter after each response, which i don't want)
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
#include <iostream>
using namespace std;
char map[15][21] = {

"+------------------+",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"+------------------+"};
int main() {
int x=3, y=3, col, row;
while (true) {
system("cls");
for(row = 0; row <= 16; row++) {
for (col = 0; col <= 20; col++)
cout << map[row][col];
cout << endl;
}
cout << "Enter WASD to go up and down left and right." << endl;
switch(getchar()) {
case 'd':
if(map[y][x+1]== '|') break;
map[y][x]=' ';
x += 1;
map[y][x] = '+';
break;
case 'a':
if(map[y][x-1]== '|') break;
map[y][x]=' ';
x -= 1;
map[y][x] = '+';
break;
case 'w':
if(map[y-1][x]== '-') break;
map[y][x]=' ';
y -= 1;
map[y][x]='+';
break;
case 's':
if(map[y+1][x]== '-') break;
map[y][x]=' ';
y += 1;
map[y][x] = '+';
break;
}
}
}

Last edited on
It's not possible.
really? well is there at least a way to make the compiler enter itself as soon as you type a character? example (i type the char 'a', and before i can type any other characters it automatically enters for me)
Last edited on
No portable way.
really, because using the second code (modefied code), I want a way to constantly check for input without pressing enter.
C++ doesn't know what a keyboard is (and indeed, does not require you to even have a keyboard). Ultimately, what you're rtying to do here is ask the operating system to tell you about the keyboard. Different OS - different way of asking. Buried inside conio will be some specific way(s) of asking.
Last edited on
only reason i was trying to know was because im trying to use only c++ headerfiles, because if i use conio.h wouldnt it limit it to certain operating systems, or compilers
Does cin support input a character without pressing Enter key?
According to them it does not, so is there a way to make it so that it only allows inputs during a select amount of milliseconds and after it times out the line will automatically end.
Using conio.h does restrict you to using Borland on DOS.
really? well is there at least a way to make the compiler enter itself as soon as you type a character? example (i type the char 'a', and before i can type any other characters it automatically enters for me)

I don't understand what you want.

really, because using the second code (modefied code), I want a way to constantly check for input without pressing enter.

(Pretty sure) I understood you correctly. Do you want to check the "cin->string" (The last character pressed) ?

Edit :
after it times out the line will automatically end.

I'd like to know more information.

Think : CreateThread may be a good idea.
Last edited on
Dealing with unbuffered input and full-screen terminal output is necessarily an OS/terminal-dependent issue. However, there are ways to make doing this cross-platform significantly easier.

I recommend you check out the NCurses library. It was designed to do exactly these things. PDCurses is the Windows version of the same library -- code written for NCurses will compile under PDCurses (except for a very few special cases carefully noted in the documentation).

NCurses (POSIX)
http://www.gnu.org/software/ncurses/

PDCurses (Windows)
http://pdcurses.sourceforge.net/

Getting Started
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

Some links to links to examples I have posted in the past:
http://www.cplusplus.com/forum/general/51564/#msg280396
http://www.cplusplus.com/forum/general/497/#msg1734

Well, that's all I care to google atm. Good luck!
Topic archived. No new replies allowed.