ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Originally posted by blizunt7 Hey all,
I want to be able to wait for ANY key press to continue execution of my program.
I have tried getch(), cin.get(), getchar()
NOthing is working.
I am using Linux g++ (gcc) version 3.4.2
All i want to do is, prompt to press anykey.
Upon ANY key press simply continue execution.
Any help would be great!! also what include files might i need to implement this??
Thank you very much
Josh
This is a hard one.
This has nothing to do with gcc or any other compiler but with the terminal/
All the input is buffered and send to a program only when enter is pressed no matter if you are reading the input in your program character by character or not. To get an unbuffered input which means get a character just after the key is pressed you need to mess with the terminal or use ncurses. Read here for more http://krtkg1.rug.ac.be/~colle/C/get...out_enter.html
Last edited by perfect_circle; 07-19-2005 at 06:50 PM.
Originally posted by blizunt7 what is -lcurses?????
and what are all the functions calls before it?
thanls
-lcurses or -lncurses is the way to tell to your compiler to link your code against the libncurses library.
You'll need it if you want to use the ncurses functions.
Do
Code:
man ncurses
and you'll find out what those function calls are. (or follow the link I've posted you before)
"initscr is normally the first curses routine to call when initializing a
program. ..................................
...............................................
The initscr code determines the terminal type and initializes all curses
data structures. initscr also causes the first call to refresh to clear
the screen."
"The cbreak routine disables line buffering and
erase/kill character-processing (interrupt and flow control characters
are unaffected), making characters typed by the user immediately avail-
able to the program."
"The printw, wprintw, mvprintw and mvwprintw routines are analogous to
printf [see printf(3S)]. In effect, the string that would be output by
printf is output instead as though waddstr were used on the given window."
"The getch, wgetch, mvgetch and mvwgetch, routines read a character from
the window. In no-delay mode, if no input is waiting, the value ERR is
returned. In delay mode, the program waits until the system passes text
through to the program. Depending on the setting of cbreak, this is
after one character (cbreak mode), or after the first newline (nocbreak
mode). In half-delay mode, the program waits until a character is typed
or the specified timeout has been reached."
"A program should always call endwin before exiting or escaping from
curses mode temporarily. This routine restores tty modes, moves the cur-
sor to the lower left-hand corner of the screen and resets the terminal
into the proper non-visual mode. "
"A program using these routines must be linked with the -lncurses option,
or (if it has been generated) with the debugging library -lncurses_g.
(Your system integrator may also have installed these libraries under the
names -lcurses and -lcurses_g.) The ncurses_g library generates trace
logs (in a file called 'trace' in the current directory) that describe
curses actions."
Hope I made things clearer.
Last edited by perfect_circle; 07-19-2005 at 11:44 PM.
Originally posted by lowpro2k3 I really must disagree against using ncurses and rewriting all your output code just so you can "wait for any key".
Code:
char t;
cout << "Press a key then press enter: "
cin >> ch;
Its much more portable using C++ for simple stuff. I do believe ncurses has its uses, but this isnt one of them.
*steps into flamesuit*
He just asked how to do it. I posted him a link with 2 ways on how to get input without pressing ENTER (messing with the terminal or using ncurses), and then I posted him an example of ncurses.
All I wanted to show him is that this is feasible if he can't live without it, that the buffered input has to do with the system and not the compiler, and I just adviced him to not mix normal I/O with ncurses if he choose to use ncurses.
There is nothing to flame or argue about. We really don't know if he is using C, or C++, or if he is mixing both which is a bad thing to do. We also don't know if he started the program for something serious or if he is just playing and programming something simple for fun.
Personally I prefer C for simple tasks but that's a different story.
If you are saying that using ncurses for something like that is to much mess for nothing, then I agree 100%.
But if he is just playing and wants to learn how to code then why not use ncurse?
A similar problem i found when i was doing an excercise from Bruce Eckels "thinking in C++" (which is currently on hold due to too much work)
you had to read in a file, then play it back, an enter between each line. Closest i got, which uses C++ libs instead of C was:
Code:
//: Chapter 2 excercise 7 from ticpp1
// Program to list a file, waiting for an "enter" at each line to continue
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
ifstream in ("Fillvector.cpp");
char cr;
string line;
cout << "Fillvector will be displayed,"
"press enter after each line for the next" << endl;
while (getline(in,line)) {
cout << line << endl;
cin.get(cr);// enter (works with any char though, ought to fix this, but it'll do for now);
}
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.