RFC: C++ "Hit any key to continue..." solution

This is a discussion on RFC: C++ "Hit any key to continue..." solution within the C++ Programming forums, part of the General Programming Boards category; So, I've been searching for a cross platform way (works on UNIX and Windows) to implement "Hit any key to ...

  1. #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?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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> >'

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    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

  4. #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

  5. #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

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by major_small
    eh, what's wrong with cin.get()?
    What about cin.ignore()?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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
    Last edited by The Brain; 09-02-2004 at 08:36 AM.
    • "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

  8. #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.

  9. #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

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    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

  11. #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

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RFC - Simple Encryption
    By Nor in forum C++ Programming
    Replies: 11
    Last Post: 02-16-2009, 03:59 PM
  2. "action on key hold" programs?
    By siddharth2212 in forum C++ Programming
    Replies: 11
    Last Post: 07-29-2007, 11:19 AM
  3. Replies: 1
    Last Post: 10-07-2001, 12:27 AM
  4. Pause or press any key to continue
    By muffin in forum C Programming
    Replies: 3
    Last Post: 09-12-2001, 12:26 PM

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