03 Jan, 2010, BrainFRZ wrote in the 1st comment:
Votes: 0
Hey again, sorry to bother you guys with yet another problem. =\

I'm trying to write a pause() function that will work on UNIX. I know that, with Windows, one can just write system("pause"). However, since system() only pushes directly to the OS's command line, this method is highly platform dependent. Also, system("pause") has a predefined message that can't be customized. My solution was this:
void pressKeyToExit()
{
cout << "Press any key to exit." << endl;
getc(stdin);
}

However, with this approach, I still needed to press enter. In other words, getc(stdin) behaved identically to getline(cin, tempBuf). I tried using getchar(), but this also had the same effect.

Does anyone have any suggestions on how to emulate Windows's "pause" command in standard C++? Also, does anyone have any ideas on why getc/getchar require a carriage return?
03 Jan, 2010, Omega wrote in the 2nd comment:
Votes: 0
could it be your terminal that your using isn't initiating the command until return is pressed?
03 Jan, 2010, quixadhal wrote in the 3rd comment:
Votes: 0
You need to put the terminal's stdin into raw (unbuffered) mode to be able to fetch data as it becomes available. By default stdin and stdout are buffered and filtered.

Try "man termios".

Specifically, you want to clear the ICANON bit, although I see there's now a handy utility function cfmakeraw() which does that and some other stuff you'll probably also want.
03 Jan, 2010, BrainFRZ wrote in the 4th comment:
Votes: 0
Thanks for the help! I looked at cplusplus.com, and found this post on termios: "not using enter" - jsmith. You'd think there would be something in the documentation on getc/getchar about termios… Anyway, thanks again for the help. :)

Btw, is there a way of making something work on BOTH Linux AND Windows? Or is something like that impossible?
03 Jan, 2010, Kline wrote in the 5th comment:
Votes: 0
In my entire lack of experience writing for Windows portability …. #ifdef is your friend ;) That's the best I can offer :D
03 Jan, 2010, Twisol wrote in the 6th comment:
Votes: 0
It's very possible, I've done it myself using the Win32 console functions. But it's absolutely 100% different from the Linux method… as Kline said, #ifdef is your friend!

http://msdn.microsoft.com/en-us/library/...

What I had to do was create a waitable timer that fired every so often, and I polled the keyboard for changes every time it went off. I wrote a Keyboard class to handle some details, like if a key is tapped and released between polls, it still marks it as pushed temporarily, then resets it; but if a push lasts for at least a poll, it maintains it until the release is reached.
03 Jan, 2010, Tyche wrote in the 7th comment:
Votes: 0
BrainFRZ said:
Btw, is there a way of making something work on BOTH Linux AND Windows? Or is something like that impossible?

#ifdef __WIN32__
#include <conio.h>
#else
//unix stuff
#endif
#include <iostream>

int main()
{
char ch;
#ifdef __WIN32__
ch = getch();
#else
//unix stuff
#endif

std::cout << ch << std::endl;
return 0;
}
03 Jan, 2010, Twisol wrote in the 8th comment:
Votes: 0
conio.h isn't even a standard header, and it's implemented differently depending on the vendor (you can't even count on some functions being supplied). I'd personally stay away from it if possible.

EDIT: You could try looking into ncurses.
04 Jan, 2010, Tyche wrote in the 9th comment:
Votes: 0
Twisol said:
conio.h isn't even a standard header, and it's implemented differently depending on the vendor (you can't even count on some functions being supplied). I'd personally stay away from it if possible.


We're already into non standard C territory. The poster might find more pragmatic to just use getch(), rather than attempting to find a Windows compiler vendor that doesn't implement getch().
0.0/9