Thread: How to prevent program termination after execution

  1. #1
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52

    How to prevent program termination after execution

    People..this must be quite old by now..but I need to know, is there any other way to stop the program from terminating after execution,besides cin.get(); or system("PAUSE");?

    system commands are hackable,so I'd prefer a C++ way..

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    what's wrong with cin.get();? That's not a system comand!

    Look in the faq - faq.cprogramming.com

  3. #3
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Dunno it wasn't working..ok I'll check it.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It was probably getting a newline left in the input buffer.
    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

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Quote Originally Posted by SVXX View Post
    People..this must be quite old by now..but I need to know, is there any other way to stop the program from terminating after execution,besides cin.get(); or system("PAUSE");?

    system commands are hackable,so I'd prefer a C++ way..
    Noobs *rolleyes*
    I wonder why the program terminates, though?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Assuming Windows you don't need to do anything to keep the program open. Just run your programs from the Command Prompt window which stays open so you can rerun the program or type another command.

    I have a few shortcuts to it, all configured to open in directories where my programs are, so I wouldn't have to cd a lot to get to these directories.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    have you tried:
    Code:
    std::cout << "Press [Enter] to continue...";
    cin.ignore(256, '\n');

  8. #8
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    There's also getch.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Noobs *rolleyes*
    Um, I fail to see what the problem is that warrants this kind of response.

    >cin.ignore(256, '\n');
    >There's also getch.
    Let's not keep it so simple that we produce bad code, mmkay? Here are the issues:

    1) The program is running interactively, so the user needs to see any output.
    Obviously, if the program terminates and closes the window before the user has time to read any output, that's an issue.

    2) The program is running in a separate process with its own console window.
    If the program owns the window and the process terminates, the window will be destroyed, thus losing any of the output before the user can read it.

    3) The program is performing unclean input that might leave junk in the stream.
    This happens when you mix formatted and unformatted input, cin's >> operator and cin.get, for example.

    4) The program uses a blocking single character read to stop execution.
    If there are any characters in the stream, this read will succeed immediately and there won't be any blocking.

    The solution is to discard leftover characters in the stream if they exist so that the blocking read blocks, but only if the program is running interactively in a separate process. Here's a largely portable solution that covers all of the bases, but it's kind of voodooish:
    Code:
    #include <cstring>
    #include <iostream>
    #include <ios>
    #include <istream>
    #include <limits>
    
    namespace jsw {
      template <typename CharT, typename Traits>
      std::basic_istream<CharT, Traits>& ignore (
        std::basic_istream<CharT, Traits>& in, CharT delim )
      {
        if ( in.rdbuf()->sungetc() != Traits::eof() && in.get() != delim )
          in.ignore ( std::numeric_limits<std::streamsize>::max(), delim );
    
        return in;
      }
    }
    
    int main ( int argc, char *argv[] )
    {
      bool interactive;
      bool owns_console;
    
      for ( int i = 1; i < argc; i++ ) {
        if ( std::strlen ( argv[i] ) == 2 ) {
          if ( argv[i][0] == '-' ) {
            if ( argv[i][1] == 'i' )
              interactive = true;
            else if ( argv[i][1] == 'o' )
              owns_console = true;
          }
        }
      }
    
      // Your program goes here
    
      if ( interactive && owns_console ) {
        std::cin.clear();
        jsw::ignore ( std::cin, '\n' );
        std::cout<<"Press Enter to continue . . .";
        std::cin.get();
      }
    }
    The problem is that while you can portably control the stream contents (sort of), you can't portably tell with code how the program is being run. So you can use parameters to tell that. When you have problems keeping the output window open, just add -i and -o to the run string:
    Code:
    myprog -i -o
    As a side note, anything besides the following two options is probably a poor choice when it comes to clearing an unfinished line from the input stream:
    Code:
    std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
    Code:
    char ch;
    
    while ( std::cin.get ( ch ) && ch != '\n' )
      ;
    That means getch is a poor choice (Yarin), and random magic numbers for the ignore amount are a poor choice (sh3rpa), and system("PAUSE") is an extremely poor choice for various reasons (SVXX). All in all, this is a deceptively simple operation, but it's very easy to step into the realm of non-portable behavior.
    My best code is written with the delete key.

  10. #10
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Hmm..so can I make this a header file and include it in my main file?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Hmm..so can I make this a header file and include it in my main file?
    No, you're not allowed to customize anything in C++. Any code you see is written in stone and can't be changed.

    I'm joking, by the way. In fact, if you know that your program won't be used as a filter (ie. cin and cout won't be redirected) to non-interactive devices, you can omit the switch stuff in your main program:
    Code:
    #ifndef JSW_STREAM
    #define JSW_STREAM
    
    #include <ios>
    #include <istream>
    #include <limits>
    
    namespace jsw {
      template <typename CharT, typename Traits>
      std::basic_istream<CharT, Traits>& ignore (
        std::basic_istream<CharT, Traits>& in, CharT delim )
      {
        if ( in.rdbuf()->sungetc() != Traits::eof() && in.get() != delim )
          in.ignore ( std::numeric_limits<std::streamsize>::max(), delim );
    
        return in;
      }
    }
    
    #endif
    Code:
    #include <iostream>
    #include "jswstream.h"
    
    int main()
    {
      // Your program goes here
    
      std::cin.clear();
      jsw::ignore ( std::cin, '\n' );
      std::cout<<"Press Enter to continue . . .";
      std::cin.get();
    }
    My best code is written with the delete key.

  12. #12
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    O.O nice Ima implement this...thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program hangs after execution
    By nizbit in forum C Programming
    Replies: 6
    Last Post: 04-16-2005, 09:52 AM
  2. How to prevent exiting from running program
    By scorpio_IITR in forum Linux Programming
    Replies: 5
    Last Post: 01-18-2004, 04:15 AM
  3. Visual C++ Program Termination
    By Padawan in forum C++ Programming
    Replies: 15
    Last Post: 11-26-2003, 11:05 PM
  4. abnormal program termination
    By Smiley0101 in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2003, 05:04 PM
  5. Problems with Getch() program execution order
    By napkin111 in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2002, 01:44 PM