What's with System("PAUSE");

This is a discussion on What's with System("PAUSE"); within the C++ Programming forums, part of the General Programming Boards category; Ok, some of us programmers have this problem, where the Win32 Console Application Flashes Open and then Closes Instantly afterwards...why?? ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103

    What's with System("PAUSE");

    Ok, some of us programmers have this problem, where the Win32 Console Application Flashes Open and then Closes Instantly afterwards...why?? Sure we can fix it with
    System("PAUSE"), cin.get(), and/or cin.ignore()...but why does this happen? Is the OS Version that is being comipiled affects it somehow? Why so? Why some compilers don't do it, and some others do? Is there an option that we can modify to let us do return 0; only and not close by itself? Does Someone have or know the reason...please, some of programmers are agitated because of this. Some of us Programmers need to know.

  2. #2
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    The reason this happens is because once main returns, the task is considered finished. It's the same with adding something like cin.get(). The task is not finished until main returns, and obviously the last thing you have to do is press enter.

    just run it in Command Prompt if you want to see the result without adding cin.get().

    but really, what's one line of code?
    Last edited by dra; 08-07-2005 at 09:27 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    When you run an application meant for the windows console, it does its thing and then exits. So, if you run this executable normally (e.g. by double-clicking on the .exe file), then windows will open a console window specifically for your program, then close it when the program is done. If however, you open a console window (e.g. with the cmd command), then run your program from the command prompt, that console window stays open, since it was not created just for your program.

    When running your program from an IDE, some IDE's have the extra feature to create a special console window that adds the equivalent of "PAUSE" after your program runs. So the natural behavior of a console window opened specifically for your program is to close, and some IDE's provide the feature to leave it open.

    If you don't like it, and your IDE doesn't have the automatic pause feature, then open a console window to the location of your executable. Run the exe from that console window when you want to run it. You can even hit the up arrow after you do it once so you don't have to type in the name every time. That window will stay open, and all the output from all your previous runs will be there also.

    You should also get familiar with your debugger. If you do, you will find that a breakpoint at the end of your main function is also effective.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just picking up on some points that (I think) others have left behind:
    Quote Originally Posted by toonlover
    System("PAUSE"), cin.get(), and/or cin.ignore()...
    don't use system() - it introduces possible security concerns and makes your code non-portable. use cin.get(). cin.ignore() wouldn't do anything in this case.
    Quote Originally Posted by toonlover
    Is the OS Version that is being comipiled affects it somehow? Why so?
    kinda, but not really. alot of coders on linux use g++, which is a command-line compiler. when they run their code on the command line, the console stays open after their code runs. it's the same as running command on windows and running your program from there.
    Quote Originally Posted by toonlover
    Is there an option that we can modify to let us do return 0; only and not close by itself?
    well, once your program returns, it's done. the operating system now has control again. if you're in a console, it gives you another opportunity for input. if you invoked your program by double-clicking, etc, the console was opened specifically for the purpose of running your program. once your program is done, the OS has no further reason to keep that console around, so it gets rid of it.
    Quote Originally Posted by toonlover
    Does Someone have or know the reason...please, some of programmers are agitated because of this. Some of us Programmers need to know.
    it's not really anything to get agitated over. if it weren't for this, you'd have to close two programs for every one you opened.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

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