exit program

This is a discussion on exit program within the C Programming forums, part of the General Programming Boards category; what is another alternative to Code: system("pause"); using a standard c library function I need to get this to work ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    17

    exit program

    what is another alternative to

    Code:
    system("pause");
    using a standard c library function

    I need to get this to work =(

    Code:
    printf ("Press enter to close window...");
    	scanf (" %c", &close);
    	if(close == '\n')
    		EXIT_SUCCESS;
    	EXIT_FAILURE;
    	exit(EXIT_SUCCESS);
    Last edited by Teardrop3903; 03-10-2011 at 01:43 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The safest thing to do, if you MUST use scanf(), is to immediately remove the left over newline char, from the input buffer:

    Code:
    scanf("%s", string)
    getchar(); //removes the newline char, will NOT halt and wait for input
    scanf("%c", &myChar) //now myChar has clean input steam.
    (void) getchar(); //another way, if the plain getchar() gives you warnings
    
    /* to remove several char's from the input stream */
    while((ch=getchar()) != '\n');
    
    system("pause"); // is a poor habit to get into.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    I'm not really understanding it

    Code:
            getchar();                                                    //clears previous newline char
            printf ("Press enter to close window...");
    	scanf ("%c", &close);
    
            //while ( getchar() != '\n' );
    should i be doing the EXIT_SUCCESS afterwards?

    I got mines to work, this is what i did.

    Code:
    while ( getchar() != '\n' );
    
    printf ("Press enter to close window...");
    scanf ("%c", &close);
    if(close == '\n')
    return 0;
    Thanks for the help. =)
    Last edited by Teardrop3903; 03-10-2011 at 02:25 PM.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    exit(1) to exit from....
    getch() works exactly like system("pause")

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Mr.777 View Post
    exit(1) to exit from....
    getch() works exactly like system("pause")
    getch() is not a standard C function -- read the original post.

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by zacs7 View Post
    getch() is not a standard C function -- read the original post.
    Huh......... Can't you use console library then??

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    what is another alternative to

    system("pause");
    using a standard c library function
    You can use getchar(). or run your program from shell/cmd.
    getch()/conio.h is non-standard library/function. You should avoid using it unless you really need it.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I use this all the time, to hold open the console window in Turbo C:

    Code:
    at the end of main:
      printf("\n\n\t\t\t    press enter when ready"); //to center it, use three tabs and four spaces before "press"
      (void) getchar(); //the void part eliminates a warning
      return 0;
    } //end of main
    
    
    /* rarely, when the input stream might get really dirty, I'll use this */
    
      printf("\n\n\n\t\t\t    press enter when ready");
      while((ch= getchar)) != '\n'); 
      (void) getchar(); //the void part eliminates a warning
      return 0;
    } //end of main
    When you get a chance though, try out Pelles C -- woooo! It's nice!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 10:13 PM
  2. HELP with Program!
    By afnitti in forum C Programming
    Replies: 9
    Last Post: 04-15-2009, 08:06 PM
  3. exit a program at any point.
    By slightofhand in forum C Programming
    Replies: 5
    Last Post: 03-02-2008, 08:08 AM
  4. Program Terminating With Error On Exit
    By chriscolden in forum C Programming
    Replies: 19
    Last Post: 01-14-2006, 03:40 AM
  5. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 03:13 AM

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