Thread: Replacing system("PAUSE")

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    Replacing system("PAUSE")

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is covered in the FAQ, actually: How do I get my program to wait for a keypress?.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Thanks

  6. #6
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    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 <_<

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    You can also output what you want to know to a file.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Hussain Hani View Post
    but i dont advice you to use it to be honest. "I think" it has performance issue
    You're concerned about a performance issue in a function that is being used to wait for the user to exit the program?

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by brewbuck View Post
    You're concerned about a performance issue in a function that is being used to wait for the user to exit the program?
    LOL.

    But seriously, can someone explain to me why people insist upon using the non-portable getch() and conio.h? What would kill them to just use std::cin.ignore() or fgetc(stdin) to read off a single char?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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?

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, here's the POSIX version, C99 code:
    Code:
    #include <unistd.h>
    #include <termios.h>
    #include <string.h>
    
    void pause(const char *msg)
    {
      printf("&#37;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);
    }
    I'm sure someone can write a WinAPI version that does the same: print a message, wait for a single keypress.
    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

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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;
    }

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2008, 05:56 PM
  2. Replacing a notebook drive
    By RoD in forum Tech Board
    Replies: 2
    Last Post: 03-29-2005, 07:07 AM
  3. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  4. Replacing string in file
    By Spedge in forum C Programming
    Replies: 1
    Last Post: 08-19-2003, 02:53 AM
  5. replacing a part of a string
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-20-2002, 03:32 AM