LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   input of a character from console without pressing enter (https://www.linuxquestions.org/questions/programming-9/input-of-a-character-from-console-without-pressing-enter-676056/)

Bonny 10-13-2008 09:54 AM

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!

Matir 10-13-2008 11:51 AM

You have to disable line buffering for the terminal.

AceofSpades19 10-13-2008 12:23 PM

You have to include ncurses.h in order to use getch()

sundialsvcs 10-13-2008 01:02 PM

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...)

Bonny 10-13-2008 01:04 PM

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?

AceofSpades19 10-13-2008 04:09 PM

Quote:

Originally Posted by Bonny (Post 3308874)
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?

you need to link it to the ncurses library
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++

sventrn 10-19-2008 04:25 AM

getchar();
 
Quote:

Originally Posted by Bonny (Post 3308745)
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 add this lines:
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?

Bonny 10-28-2008 01:00 PM

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?

AceofSpades19 10-28-2008 04:50 PM

Quote:

Originally Posted by Bonny (Post 3324216)
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?

its because its in a namespace, you need to do std::cout


All times are GMT -5. The time now is 06:27 PM.