reading a charater without carriage return

hi everyone this is my first time that i m wriring in this forum.......

presetly i m persuing a B.Tech degree ( 1st year ) and thus i m in to a lot of programming .......

due to which i hv encountered alot of problems that i would like to be solved as i was not able to find the solutions of these........

for instance.....in a normal c++ program i was required to take a chracter as an input and process the input without carriage return i.e. as soon as the the character is pressed from the keyboard the required processes take place.....

in complete reference c++ i read that in the header file conio.h the function getch() does the same function as required by me .........

but when i used it then it showed that no such header file exists...

so now if anyone can solve my dilemma then he is of course welcome........
You have to play with the OS to do that. The NCurses (Unix/Linux/OS X) or the equivalent PDCurses (DOS, Win, OS/2) libraries make it very easy to write cross-platform code that will do this.

If you want to get into the nitty gritty, play with SetConsoleMode on Windows. It is a little more involved on POSIX platforms. Let me know if you want to do that.

(I still recommend you use Curses, because it has nice facilities to translate function key code sequences into a single value using wgetch().)

Hope this helps.
i exactly do not understand as what do u mean by curses as i hv never come across such a word....so please if u can elaborate on this than it might be of help....

thanx for ur reply...
Google "ncurses" and see what you'll find...
Last edited on
Hello!! I've got the answer to do that in that web page: (WITHOUT USING NCURSES!!!)
http://airamrguez.blogspot.com/2008/02/cmo-implementar-la-funcion-getch.html
Last edited on
I hate to burst your bubble, but that only works on Unix, it cheats, it is chock-full of dangerous security holes, it creates extra files wherever it is used (kind of like having a dog that poops all over your house) without cleaning up after itself, and it will fail if the file already exists (or, fixing that, it could destroy a legitimate file).

It is easy, though. If you are not doing this just for a homework assignment, then be aware that you are doing something that could get you fired for the security problems alone, if not the others mentioned.


NCurses really isn't that hard to use, and it will allow you to get non-buffered input while properly translating function key sequences into a single keycode, and it is very portable. Type "man initscr" at the terminal to get started.

If you want a simple example, let me know.
OK, I decided to post a simple example anyway. Since this is a C++ forum, I've used a std::string to highlight some steps necessary to pass them to the curses routines.
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 <string>
#include <curses.h>

std::string name_key( int c ) {
  // This function takes a keycode and turns it into a string.
  // I'm pretty sure there is a curses function to do this,
  // but I'm not going to look it up right now... (sorry)
  std::string result;
  if (c < 32) {  // control keys
    result = "^";
    result += (char)c +'A' -1;
    }
  else if (c == 127) result = "^?";
  else if (c > 127) result = "FN";  // any function key (arrows, F1, Cut, etc)
  else result = c;  // normal alphanumeric key
  return result;
  }

int main() {
  std::string s = "Press the 'any' key...";
  int c;

  // Initialze
  initscr();
  raw();			  // line-buffering off, echo off, etc.
  nonl();			  // NL conversions off
  intrflush( stdscr, FALSE );	  // we want ^C, etc...
  (void) keypad( stdscr, TRUE );  // extended keys ON

  curs_set( 1 );		  // make sure the cursor is visible

  // clear the screen
  wclear( stdscr );

  // move the cursor to (x, y) = (10, 5)
  wmove( stdscr, 5, 10 );

  // display our message (curses routines are not prototyped with const)
  waddstr( stdscr, const_cast<char*>(s.c_str()) );

  // update the screen so that our changes are visible
  wrefresh( stdscr );

  // wait for the user to press a single key
  c = wgetch( stdscr );

  // complain that the user did not press the 'any' key
  wmove( stdscr, 7, 10 );
  s = std::string( "Hey, you pressed the '" ) +name_key( c ) +"' key!";
  waddstr( stdscr, const_cast<char*>(s.c_str()) );

  // wait for the user to quit
  wmove( stdscr, 20, 0 );
  waddstr( stdscr, "Press any key to quit..." );
  wrefresh( stdscr );
  wgetch( stdscr );

  // restore the terminal to its proper state and quit
  endwin();
  return EXIT_SUCCESS;
  }


To compile this code on Windows, I assume that you have PDCurses installed.
g++ example.cpp -lpdcurses

On Linux and many if not most Unices:
g++ example.cpp -lcurses

If it makes a difference on your version of Unix (because it is linking with the old Curses library instead of the desired NCurses library):
g++ example.cpp -lncurses

If you are linking on Solaris or some other system where NCurses was installed with the --disable-overwrite option, you'll need to tell your compiler where to find the correct "curses.h" header file:
g++ example.cpp -I/usr/local/include/ncurses -lncurses

Hope this helps.
Last edited on
Topic archived. No new replies allowed.