LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-13-2008, 09:54 AM   #1
Bonny
Member
 
Registered: Jul 2005
Location: UK
Distribution: Slackware v12.1
Posts: 40

Rep: Reputation: 15
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!
 
Old 10-13-2008, 11:51 AM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
You have to disable line buffering for the terminal.
 
Old 10-13-2008, 12:23 PM   #3
AceofSpades19
Senior Member
 
Registered: Feb 2007
Location: Chilliwack,BC.Canada
Distribution: Slackware64 -current
Posts: 2,079

Rep: Reputation: 57
You have to include ncurses.h in order to use getch()
 
Old 10-13-2008, 01:02 PM   #4
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,268

Rep: Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086
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...)
 
Old 10-13-2008, 01:04 PM   #5
Bonny
Member
 
Registered: Jul 2005
Location: UK
Distribution: Slackware v12.1
Posts: 40

Original Poster
Rep: Reputation: 15
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?
 
Old 10-13-2008, 04:09 PM   #6
AceofSpades19
Senior Member
 
Registered: Feb 2007
Location: Chilliwack,BC.Canada
Distribution: Slackware64 -current
Posts: 2,079

Rep: Reputation: 57
Quote:
Originally Posted by Bonny View Post
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++
 
Old 10-19-2008, 04:25 AM   #7
sventrn
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0
Talking getchar();

Quote:
Originally Posted by Bonny View Post
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?
 
Old 10-28-2008, 01:00 PM   #8
Bonny
Member
 
Registered: Jul 2005
Location: UK
Distribution: Slackware v12.1
Posts: 40

Original Poster
Rep: Reputation: 15
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?
 
Old 10-28-2008, 04:50 PM   #9
AceofSpades19
Senior Member
 
Registered: Feb 2007
Location: Chilliwack,BC.Canada
Distribution: Slackware64 -current
Posts: 2,079

Rep: Reputation: 57
Quote:
Originally Posted by Bonny View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to simulate a ENTER key pressing khaos83 Linux - Newbie 2 08-06-2008 04:41 AM
Leave vi mode without pressing enter ? Vilius Linux - General 4 11-13-2007 04:12 AM
Problem in getting telnet client prompt by pressing Escape character( ctrl -] ) Prassanta Suse/Novell 0 01-23-2007 12:33 AM
Mandrake 10 doesnt install after pressing enter subliminal Mandriva 9 06-17-2004 03:04 PM
I did the following program and get blank line after pressing <ENTER> purpleburple Programming 5 08-21-2002 12:00 PM


All times are GMT -5. The time now is 02:00 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration