Thread: Quitting a program

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8

    Post Quitting a program

    I have used this before, but I can't remember how to do it. In a console application, how do you quit using a system command? for example the
    Code:
    system("SOMETHING");
    is the way I remember. Also, how do you use that same system function to clear the screen?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    exit - system("exit"); I think is what you're looking for. Are you just trying to quit your program at a particular point? And system("cls"); clears screens, but there's a better way from the FAQ. If you're using Windows, the last is the best option. Just copy and paste the function.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Here is the deal... if you call a program with system, the calling program can't do anything until the program called is completed and the system call resolves. This includes attempting to close the program before it actually completes itself. This is one of the many reasons not to use system().

    As for clearing the screen... there is no standard way. Check the FAQs... it has an entry on clearing the screen. It also talks about using system() in "Running a program from within a program".
    Sent from my iPadŽ

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Well to quit a program just return 0; in main().

    Easier than doing some fancy stuff.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    In fact returning from main() is by far the best way to exit a program; in C++ you should use nothing else. The reason is that in C++, unlike in C, you have objects with destructors that need to be called when the variables go out of scope.

    For example, if your code writes to files, it would be rather nice to make sure the most up-to-date data was flushed to disk before your program ends, wouldn't it? ofstream's destructor will do that for you. If you bypass it with some C command, that doesn't happen.

    Let the program die with dignity and grace, like it's supposed to.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Why don't you just do this?

    Code:
    exit(0);
    It is simple and effective

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by swgh
    Why don't you just do this?

    Code:
    exit(0);
    It is simple and effective
    It's also very bad, for exactly the reason I mentioned in the post before yours. exit() does not unwind the stack; it does not call destructors for objects. Any resource you allocated (which should be freed in some destructor somewhere) remains allocated. C++ file streams will not be flushed, resources you acquired from the OS will not be returned, cats and dogs will live together, etc.

    Yes, it works in certain situations in certain kinds of code, but it's a very bad habit to get into.

    This is included ONLY for backwards compatibility with C (it's a C function) -- and C didn't have destructors. In general it's one of those lines of code that simply shouldn't be used in C++.

    If you want an equivalent in C++, do something like this:

    Code:
    class ExitException{
    public:
         ExitException(int c){ m_c = c;}
         int GetCode(){ return m_c;}
    private:
         int m_c;
    };
    
    
    ...
    
    int main(){
         try{
    
         /* Body of your code goes here */
    
         }
         catch(ExitException e){
              return e.GetCode();
         }
    }
    
    
    ....
    
         /* Inside some function, and we want to exit on an unrecoverable error */
        throw ExitException(1);
    That does the same sort of thing but DOES unwind the stack, and all variables going out of scope have their destructors properly called.

    You could simplify this by just throwing an int to begin with, although I prefer not throwing primitives. Under that case it would look like:

    Code:
    int main(){
         try{
    
         /* Body of your code goes here */
    
         }
         catch(int i){
              return i;
         }
    }
    
    
    ....
    
         /* Inside some function, and we want to exit on an unrecoverable error */
        throw 1;
    Last edited by Cat; 09-30-2006 at 03:48 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Ok Cat, I think Il throw exceptions instead of using exit(0) in the future, thanks for pointing this out, I did not know about the stack issue. You learn somthing new everyday! lol
    Thanks

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by swgh
    Why don't you just do this?
    Code:
    exit(0);
    It is simple and effective
    Why can't you just use
    Code:
    return 0;
    ????
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by maxorator
    Why can't you just use
    Code:
    return 0;
    ????
    You can, if you're in main().
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Quitting a Program.
    By Ranedhel in forum C++ Programming
    Replies: 4
    Last Post: 06-17-2003, 03:38 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM