C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-01-2004, 01:45 PM   #1
Registered User
 
Join Date: Sep 2004
Posts: 2
RFC: C++ "Hit any key to continue..." solution

So, I've been searching for a cross platform way (works on UNIX and Windows) to implement "Hit any key to continue..." functionality.

Specificaly, a better solution than any that are given here:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385

I have come up with (I think) a cross platform, C++ solution that takes care of a terminal environment that does input line buffering

Code:
#include <iostream>
using namespace std;

int main() {
    cout << "\nPress any key to continue..." << endl;
    streambuf *pbuf;
    pbuf=cin.rdbuf()->setbuf(NULL, 1);
    pbuf->sgetc();

    return 0;
}
What do people think of that? Will it work on windows?
flakier is offline   Reply With Quote
Old 09-01-2004, 02:20 PM   #2
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Doesn't compile on my machine.

error C2248: 'setbuf' : cannot access protected member declared in class 'std::basic_streambuf<char,struct std::char_traits<char> >'
bithub is offline   Reply With Quote
Old 09-01-2004, 02:23 PM   #3
C++ Developer
 
XSquared's Avatar
 
Join Date: Jun 2002
Location: UWaterloo
Posts: 2,718
It doesn't compile under GCC 3.3.4 on Debian Linux either:
Code:
matt@debianbox c $ g++ -pedantic -Wall flakier.cpp
/usr/include/c++/3.3/streambuf: In function `int main()':
/usr/include/c++/3.3/streambuf:713: error: `std::basic_streambuf<_CharT,
   _Traits>* std::basic_streambuf<_CharT, _Traits>::setbuf(_CharT*, int) [with
   _CharT = char, _Traits = std::char_traits<char>]' is protected
flakier.cpp:7: error: within this context
matt@debianbox c $
__________________
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
XSquared is offline   Reply With Quote
Old 09-01-2004, 02:29 PM   #4
Registered User
 
Join Date: Sep 2004
Posts: 2
Hmm, back to the drawing board!

I've got an old gcc-2.95.2.1 and at home VS.NET2002

I think I saw something on technet about streambuf::setbuf being replaced by pubsetbuf()

Thanks for the reports :)

~Jason
flakier is offline   Reply With Quote
Old 09-02-2004, 07:37 AM   #5
Registered User
 
major_small's Avatar
 
Join Date: May 2003
Posts: 2,787
eh, what's wrong with cin.get()?
__________________
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech


Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)

The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
major_small is offline   Reply With Quote
Old 09-02-2004, 07:59 AM   #6
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Quote:
Originally Posted by major_small
eh, what's wrong with cin.get()?
What about cin.ignore()?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 09-02-2004, 08:00 AM   #7
VA National Guard
 
The Brain's Avatar
 
Join Date: May 2004
Location: Manassas, VA USA
Posts: 902
with cin.get() you are restricted to only hitting, "Enter" as opposed to system("pause") which will allow you to hit any key to continue. (although it is non-portable/OS dependant)

btw I am waiting for a portable alternative to cin.get() that will allow the user to "hit any key to continue".. mainly so I can use it in my programs
__________________
  • "Problem Solving C++, The Object of Programming" -Walter Savitch
  • "Data Structures and Other Objects using C++" -Walter Savitch
  • "Assembly Language for Intel-Based Computers" -Kip Irvine
  • "Programming Windows, 5th edition" -Charles Petzold
  • "Visual C++ MFC Programming by Example" -John E. Swanke
  • "Network Programming Windows" -Jones/Ohlund
  • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
  • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Last edited by The Brain; 09-02-2004 at 08:36 AM.
The Brain is offline   Reply With Quote
Old 09-02-2004, 08:21 AM   #8
Registered User
 
Frobozz's Avatar
 
Join Date: Dec 2002
Posts: 546
You could just use preprocessors to compile one method if it is being compiled under Windows and another method if it is under Linux.
Frobozz is offline   Reply With Quote
Old 09-02-2004, 08:41 AM   #9
Registered User
 
major_small's Avatar
 
Join Date: May 2003
Posts: 2,787
Quote:
Originally Posted by The Brain
with cin.get() you are restricted to only hitting, "Enter" as opposed to system("pause") which will allow you to hit any key to continue. (although it is non-portable/OS dependant)

btw I am waiting for a portable alternative to cin.get() that will allow the user to "hit any key to continue".. mainly so I can use it in my programs
oh i see now... well, you could always use getch()... supposedly it's not portable, but I've seen it included with every compiler/linker I've ever used... and it's not OS-dependant...
__________________
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech


Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)

The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
major_small is offline   Reply With Quote
Old 09-02-2004, 10:40 AM   #10
C++ Developer
 
XSquared's Avatar
 
Join Date: Jun 2002
Location: UWaterloo
Posts: 2,718
Well, it's not included in GCC 3.3.4 on my Debian Linux box.
__________________
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
XSquared is offline   Reply With Quote
Old 09-02-2004, 10:51 AM   #11
Registered User
 
major_small's Avatar
 
Join Date: May 2003
Posts: 2,787
Quote:
Originally Posted by XSquared
Well, it's not included in GCC 3.3.4 on my Debian Linux box.
well... I didn't say I tried it on every compiler out there, did I?

i've only really tried it on an old borland compiler, Dev-C++ (migw port of gcc), MSVC (who knows), and G++ (version ?)
__________________
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech


Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)

The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
major_small is offline   Reply With Quote
Old 09-02-2004, 10:56 AM   #12
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,730
One thing to remember is that a standard solution doesn't have to preform a solution the same way on every machine, just provide a standard interface AND a standard result. How you go from the interface to the result can change depending on the environment.

As Frobozz said you can use preproccessor directives to decide which solution to use.

For a *nix solution I posted one at: faq: cached input with mygetch(). Once I find the updated code on my computer and verify that it works I'll post a more c++ based solution.
Thantos is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
RFC - Simple Encryption Nor C++ Programming 11 02-16-2009 04:59 PM
"action on key hold" programs? siddharth2212 C++ Programming 11 07-29-2007 11:19 AM
suspend for key without the ugly press any key to continue............... Stealth C++ Programming 1 10-07-2001 12:27 AM
Pause or press any key to continue muffin C Programming 3 09-12-2001 12:26 PM


All times are GMT -6. The time now is 10:29 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22