LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 09-20-2003, 07:04 PM   #1
TriggerJ
LQ Newbie
 
Registered: Sep 2003
Location: Texas
Posts: 14

Rep: Reputation: 0
C++ question


How would you program in C++ to "hit any key to conitue?"

Thanks,

J-
 
Old 09-20-2003, 07:11 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
would depend what libraries i was using, with in a basic terminal without curses i would probably just call cin.get();

there are a few good c++ tutorials on the net eg www.cplusplus.com
 
Old 09-20-2003, 07:12 PM   #3
TriggerJ
LQ Newbie
 
Registered: Sep 2003
Location: Texas
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks, I appreciate the help...I'm just starting out!
 
Old 09-20-2003, 07:26 PM   #4
nakkaya
LQ Guru
 
Registered: Jan 2003
Location: Turkey&USA
Distribution: Emacs and linux is its device driver(Slackware,redhat)
Posts: 1,398

Rep: Reputation: 45
you can decleare a variable(bogus variable) and accept that

like
int x;
cin>>x;

that will stop the programme from exiting and also there is system call
 
Old 09-20-2003, 08:59 PM   #5
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
If you want to implement a true, "Press any key to continue..." prompt, you will first have to change the way the unix terminal handles input. The standard buffered IO in un*x doesn't send the buffer to the program until a EOF or newline is entered. That is why you have to press enter if you use cin.get() or getchar, or the like. Raw (unbuffered or cooked mode) IO sends input as it is recieved, and this is what you want. You can change terminal attributes by using the stty command from inside your program. For simplicity I used the C function, system(), inside the sample code below to execute my terminal commands, but in practice you should learn to use the C exec family of functions. They are more secure and robust than system(). The code below enters raw mode, waits for one character, enters back into normal "cooked" mode, and then exits.

Code:
#include <iostream> 
#include <stdio.h>  

using namespace std;  

int main() { 
//Output prompt 
cout << "Press any key to continue..." << endl; 

//Set terminal to raw mode 
system("stty raw"); 

//Wait for single character 
getchar(); 

//Reset terminal to normal "cooked" mode 
system("stty cooked"); 

//And we're out of here 
return 0; 
}
 
1 members found this post helpful.
Old 09-20-2003, 10:05 PM   #6
TriggerJ
LQ Newbie
 
Registered: Sep 2003
Location: Texas
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks, that really helped.

Jessica
 
Old 09-20-2003, 11:24 PM   #7
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Ya welcome :-)
 
Old 12-04-2003, 12:06 PM   #8
codedv
Member
 
Registered: Nov 2003
Location: Slough, UK
Distribution: Debian
Posts: 146

Rep: Reputation: 15
On the topic of stream buffers - could you port that code to Windows/Dos for the same behaviour?
 
Old 12-04-2003, 03:02 PM   #9
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Sorry I can't. I left windows/Dos programming years ago for linux/unix programming and I dont miss a thing.
 
Old 07-22-2004, 05:07 AM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by codedv
On the topic of stream buffers - could you port that code to Windows/Dos for the same behaviour?
From what I remember abot DOS-programming, at least in Borland's Turbo C(++) you could do it simply with:
Code:
#include <conio.h>
while (!kbhit())
     ;
 
Old 07-22-2004, 06:18 AM   #11
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
wow, nearly 8 months since the last post to this thread.
 
Old 08-23-2004, 03:55 PM   #12
escale
LQ Newbie
 
Registered: Apr 2004
Location: Southwest US
Distribution: Got RH9 (Shrike) from Distro that came with Red Hat Linux 9 Bible - Christopher Negus
Posts: 8

Rep: Reputation: 0
jinksys and others:

8/23/04 14:39 MST

Some of us are just getting to this now, a year after jinksys post. This information is still valuable and a reliable reference for those who follow.

When learning a new language I flip between reference docs like man files, info files and books. Then flip to sample code snippets that people here freely post. Between these two, I will have this language mastered in two months.

Thanks kev82 for that link to: www.cplusplus.com This site has tons of stuff on the STL that I couldn't find anywhere. That site will keep me busy for couple of months.

Thanks everyone.

Well, this was a useless, generic thank you of a post, sorry, it has no useful information. Carry on elsewhere.
 
  


Reply



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
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 04:23 AM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 01:51 PM
2 part question: Speeding up MDK9.1/GNOME question wardialer Linux - Newbie 6 10-14-2004 03:16 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 02:02 PM
RE: Suse 8.0 hardware question {newbie question, pls help} Radiouk Linux - Distributions 2 06-04-2002 12:53 PM

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

All times are GMT -5. The time now is 03:12 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