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
Printable View
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?.
I looked at the link. Why we needed the myflush() for the C++ implementation?
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.
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
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);
}
What's wrong with system("PAUSE") on Windows? :p
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.
What is mypause()? It looks like you have an infinite recursive function. "pause" is used at the Windows command line to wait for a key press to continue execution of the program.
To tell the truth I am not really sure what 'mypause()' is, however, I got it from the FAQ's, to stop my command line from closing. It took me about 3 hours to find something that would work.
Oh, so mypause() refers to the example in the FAQ... it looked like you were trying to define your own function due to inconsistent indenting. As such, refer to the FAQ.
>> Or is system("pause"), something else, entirely?
It is something else entirely that attempts to do basically the same thing. It is another option for what to put inside the mypause function provided in the FAQ.