LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-19-2005, 06:25 PM   #1
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Rep: Reputation: 30
c++ g++ wait for key press???


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
 
Old 07-19-2005, 06:39 PM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Re: c++ g++ wait for key press???

Quote:
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.
 
Old 07-19-2005, 06:56 PM   #3
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
ahhhh something i did not think of, but learned in my microcontrollers class.

Use an infinite loop and just wait until the n key is pressed

Code:
while (1)
{
    if ('n' == getchar())
       break;
}
thanks!
 
Old 07-19-2005, 07:08 PM   #4
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
Originally posted by blizunt7
ahhhh something i did not think of, but learned in my microcontrollers class.

Use an infinite loop and just wait until the n key is pressed

Code:
while (1)
{
    if ('n' == getchar())
       break;
}
thanks!
I don't get what you mean. Why 'n'. Do you mean '\n' (new line)?
 
Old 07-19-2005, 07:16 PM   #5
AM1SHFURN1TURE
Member
 
Registered: Sep 2004
Location: US
Distribution: Debian
Posts: 56

Rep: Reputation: 15
though it wouldn't be portable - couldn't a system pause be used?
 
Old 07-19-2005, 07:20 PM   #6
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
system("PAUSE"); doesn't work... for some reason.
 
Old 07-19-2005, 08:13 PM   #7
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
this works for sure:
Code:
#include <curses.h>

int main(int argc, char **argv)
{
  initscr();
  cbreak();
  printw("press any key to exit...");
  getch();
  endwin();
  return 0;
}
to compile it use the -lcurses
Code:
gcc -o example example.c -lcurses
 
Old 07-19-2005, 11:21 PM   #8
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
what is -lcurses?????

and what are all the functions calls before it?
thanls
 
Old 07-19-2005, 11:32 PM   #9
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
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)
 
Old 07-19-2005, 11:41 PM   #10
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
"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.
 
Old 07-20-2005, 01:23 AM   #11
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
yes thank you!!!! explanations are always very helpfull.

Now i just have to see how i can use these robust functions. HAHA, thanks again!
 
Old 07-20-2005, 07:48 AM   #12
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
and remember: don't mix norman I/O with ncurses, which means use printw,getch but never printf,getchar in a ncurses program.
 
Old 07-21-2005, 01:50 AM   #13
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
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*
 
Old 07-21-2005, 02:21 AM   #14
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
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?
 
Old 07-21-2005, 11:25 AM   #15
-0-
Member
 
Registered: Nov 2004
Location: London uk
Distribution: Slack 10
Posts: 67

Rep: Reputation: 15
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;
}

Hope that helps.


-0-
 
  


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
Key press Majestros Programming 2 02-14-2007 03:42 AM
Double key press on Keyboard Zettai Linux - Newbie 2 03-09-2005 10:59 PM
Keyboard Problem - I have to press a key twice for certain keys daedius Linux - Hardware 4 01-13-2005 06:32 AM
Insert Boot-Disk and press any key computerlady911 Linux - Laptop and Netbook 7 10-04-2004 12:56 PM
What key to press in order to type symbols? windz Linux - General 0 02-10-2004 06:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:49 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration