input of a character from console without pressing enter
Hello. I am a newb programmer of C++. I use KDevelop 3.5.1 gcc++.
I am interested in creating a simple program that will allow the user to press a key and, without pressing enter, the computer to record that key (and use it). I know the way in normal C++ is: 'int a=getch();' and it works, but this doesn't work on KDevelop... Does anyone have any suggestions? Thank you in advance! |
You have to disable line buffering for the terminal.
|
You have to include ncurses.h in order to use getch()
|
Both of these comments are correct.
"Ordinary input" is accepted by the shell and piped to your application in a way that allows nice things like "type ahead." When you want ordinary single-character input (like "Press Enter to continue"), you simply use a standard terminal-control library ... ncurses ... to do all of this and more. Your character-based application will "just work" on a variety of types of hardware. (Yes, as a matter of fact I am old enough to remember what a real DEC VT100 looks like...) |
if I have the following code:
"#include<iostream.h> #include<ncurses.h> main(){ int valuekey; valuekey=getch(); }" I get two errors: undefined reference to stdscr and wgetch. Could anyone guide me how I could fix this issue please? |
Quote:
eg. g++ hello.cpp -o hello -lncurses You also want to use #include<iostream> instead of <iostream.h> because iostream.h is pre standard C++ |
getchar();
Quote:
in the main() { char ch; system("stty -echo"); // supress echo system("stty cbreak"); // go to RAW mode // ch = getchar(); // or something like that while ((ch = getchar()) != 'q') { ;; // DO what you want } system ("stty echo"); // Make echo work system("stty -cbreak");// go to COOKED mode } ALL "C" kompilers supported. // Do not brake it with Crtl-C. If you did so restore echo so "stty echo" in blind mode. Echo supressed. It works on all Linux systems, always. Do'nt forget to include <stdlib.h> Have sombody any Idea how to make it in Java? |
THANK YOU VERY VERY MUCH!!
It actually works !!! Thanks a million !!! :) By the way, my KDevelop 3.5.1 doesn't have iostream compatible with cout... i get an error when I try to #include<iostream> and use cout with it... Does anyone know why I have this problem? |
Quote:
|
All times are GMT -5. The time now is 06:27 PM. |