Thread: Pause a C program without getch()

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Pause a C program without getch()

    I was wondering if there is another way to prevent a C program closing when user has been inputed data other than using getch(). I do not like using it. In C++, you can do this:

    Code:
    cin.ignore();
    getchar();
    But cin is a C++ keyword. When i use getch I have to include conio.h with the program which is a bit of a waste for the purpose of 1 statement. Is there a command under the stdio,h header that can execute the same way getch() does in conio,h?

    Example:

    Code:
    #include <stdio.h>
    #include <conio.h>  // this is annoying!
    
    int main()
    {
     	int num1, num2;
     	int sec = 0;
     	
     	printf("Enter my first secret nunber: ");
    	
    	scanf("%d", &num1);
    	
    	printf("\nEnter my second secret number: ");
    	
    	scanf("%d", &num2);
    	
    	/* logical AND */
    	
    	if (( num1 == 9 ) && ( num2 == 99 ))
    	{
    	   	  printf("\nWELL DONE!");
    	   	  sec = 5;
        }
        
        /* logical OR */
    	
    	if (( num1 == 0 ) || ( num2 == 0 ))
    	{
    	   	  printf("\nNone of the numbers are zero");
    	   	  sec = 4;
        }
        
        /* logical NOT */
    	
    	if ( !sec )
    	{
    	   	 printf("\nSorry, both or either numbers are incorrect");
        }
        
        getch();
        
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    getchar();
    In stdio.h

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    You'll probably need to flush the input buffer before calling getchar
    because scanf leaves the newline character in the buffer. easy
    flush - taken from FAQ

    Code:
    int ch;
    .
    .
    .
    while ((ch = getchar()) != '\n' && ch != EOF);
    printf ("Press enter to continue...");
    getchar ();
    or even just

    Code:
    while ('\n' != getchar ());
    printf ("Press enter to continue...");
    getchar ();
    works for most, but the first is probably more correct.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    in windows you could do a system("PAUSE")

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    that'd work, but that defeats the purpose of the post: system
    is defined in stdlib.h, so you'd have to include that instead of
    conio.h... system ("pause") and getch are both non portable
    methods too, whereas the getchar approach is, and its all in
    stdio.h as requested.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why won't my program pause?
    By ladysniper in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2006, 03:22 AM
  2. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  3. pause program at console
    By datainjector in forum C# Programming
    Replies: 1
    Last Post: 03-27-2003, 12:36 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM