I know that using system("PAUSE") to pause a running program is bad. But I need it so that user can read the output. Is there any way to achieve the same effect of system("PAUSE") without using it? Thanks in advance
I know that using system("PAUSE") to pause a running program is bad. But I need it so that user can read the output. Is there any way to achieve the same effect of system("PAUSE") without using it? Thanks in advance
This is covered in the FAQ, actually: How do I get my program to wait for a keypress?.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I looked at the link. Why we needed the myflush() for the C++ implementation?
Last edited by jk1998; 05-11-2007 at 12:18 PM.
Some characters may be left on the buffer, so without such an "input flush" the next character would just be read without actually appearing to pause the program.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks
you could use <conio.h> library with
getch();
before
return 0;
but i dont advice you to use it to be honest. "I think" it has performance issue
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<
There is no reason to include a whole non Standard library just to replace system( "pause" );
Write something using Standard code, like the FAQ tells you to.
You can also output what you want to know to a file.
getch() and cin.ignore() are different. If you can use getch() on your platform, it works better and more often than cin.ignore() (or cin.get()). That's why some people prefer to use it.
I personally prefer the standard solutions because they aren't that hard to get right and I don't want to worry about who has conio and who doesn't.
Yes, they're different, but at least you know something standard works. I would imagine if it was important enough to get something like this to work exactly as you want it, shouldn't you just write your own OS specific getch() type of function to work with most operating systems that you'll target?
Well, here's the POSIX version, C99 code:
I'm sure someone can write a WinAPI version that does the same: print a message, wait for a single keypress.Code:#include <unistd.h> #include <termios.h> #include <string.h> void pause(const char *msg) { printf("%s\n", msg); struct termios trm; memset(&trm, 0, sizeof(struct termios)); cfmakeraw(&trm); struct termios old; tcgetattr(STDIN_FILENO, &old); tcsetattr(STDIN_FILENO, TCSAFLUSH, &trm); char buf; read(STDIN_FILENO, &buf, 1); tcsetattr(STDIN_FILENO, TCSAFLUSH, &old); }
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
What's wrong with system("PAUSE") on Windows?
But a little more seriously, I looked through the Windows API for console functions, and decided to try to write one up. Is there anything wrong with this that could cause any unexpected issues?
I know one "problem" is that if you're using the C (and possibly the same thing occurs with the C++ file system), the FILE * buffer maintained in the stdin FILE object are obviously not drained (or flushed as people like to say, even for input streams). That is left up to the programmer to handle.
Code:#include <windows.h> void pause(const char *szMsg) { char c; DWORD dwWritten, dwRead, dwOld; HANDLE in = GetStdHandle(STD_INPUT_HANDLE); HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); if((in) && (out)) { WriteConsoleA(out,szMsg,strlen(szMsg),&dwWritten,NULL); FlushConsoleInputBuffer(in); GetConsoleMode(in,&dwOld); SetConsoleMode(in,0); ReadConsoleA(in,&c,1,&dwRead,NULL); SetConsoleMode(in,dwOld); } return; }
>> shouldn't you just write your own OS specific getch() type of function to work with most operating systems that you'll target?
No. Most people who do this are just beginners. It is often important for them to get it working with minimal effort so they can concentrate on learning to program. If getch() works, then it works.