Like Tree3Likes
  • 1 Post By Adak
  • 1 Post By grumpy
  • 1 Post By xeon321

system("pause")

This is a discussion on system("pause") within the C Programming forums, part of the General Programming Boards category; Is there any way to replace system("pause")?...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    system("pause")

    Is there any way to replace system("pause")?

  2. #2
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Code:
    #include <stdio.h>
    
    int main()
    {
    	printf("Press any key to continue...");
    	getchar();
    	return 0;
    }
    Is my solution works fine without affect my other input?
    where is input buffer goes?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ulti-killer View Post
    Code:
    #include <stdio.h>
    
    int main()
    {
    	printf("Press any key to continue...");
    	getchar();
    	return 0;
    }
    Is my solution works fine without affect my other input?
    where is input buffer goes?
    That is what I used in Turbo C, to hold the console window open and give me a chance to read the output of the program, before it closed.

    Won't bother anything. Input from your getchar() will go STRAIGHT to byte heaven, and sing thereafter with the byte (little) Angels.
    xeon321 likes this.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    In standard C, no. Window's pause command exits as soon as a keystroke is detected, and standard C provides no means to get keyboard keystrokes directly.

    The commonly used alternative is to use a function like getchar() to read a character. Bear in mind that that getchar() will not return until the user hits the enter key (input is buffered until then). You will probably need to call it twice if preceding code uses scanf().

    The sensible way is not to run the program from inside an IDE - run it directly using a manually launched command prompt.
    ulti-killer likes this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ulti-killer View Post

    Is my solution works fine without affect my other input?
    where is input buffer goes?
    If the user doesn't use the enter key for continuing, your solution leaves at least the newline character in the input stream and thus could affect following input functions.

    I don't understand the second question.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    1) Why getchar()needs to call twice if preceding code uses scanf()?
    2) How to design to achieve
    Press any key to continue...?

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    scanf leaves a newline char behind. So if you don't have 2 getchar(), the newline will be grabbed by getchar(), and your program automatically quits.
    Adak likes this.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ulti-killer View Post

    2) How to design to achieve
    Press any key to continue...?
    You can't using just standard C.
    What you can do is "Press <Enter> to continue" and then loop until getchar() returns '\n'. All the other characters will still be visible on the screen but at least you've told the user what key to press.

    Bye, Andreas

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    Does your compiler have the non-standard header <conio.h>?

    If you do have this non-standard header, you may use the function getch(), which gets a character from stdin without waiting for enter to be hit.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Editing output message of system("pause") ?
    By alizzle in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2010, 11:52 PM
  2. is system("PAUSE") required in every program?
    By xstarburstbaby in forum C++ Programming
    Replies: 21
    Last Post: 09-03-2008, 05:18 AM
  3. system("pause") in C#? (warning: noob question)
    By Wintersun in forum C# Programming
    Replies: 20
    Last Post: 03-28-2008, 02:26 AM
  4. function to replace system("pause");
    By willc0de4food in forum C Programming
    Replies: 29
    Last Post: 09-08-2005, 12:52 PM
  5. Replies: 3
    Last Post: 06-01-2004, 04:29 PM

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