none
system("Pause")

    Question

  • Why do I have to enter system("Pause"); each time I run a program in Visual C++ Express 2010 and sometimes with Visual C++ Express 2008?

    Many thanks.

    springwater500

    Thursday, May 13, 2010 7:59 PM

Answers

  • No, your system is working properly. It only pauses if you start your application without debugging. (Press Ctrl+F5 instead of F5, or go to Debug->Start Without Debugging). When the debugger is attached it doesn't automatically pause.

    But by doing this you won't have the VS debugger attached so you won't be able to break into your application or have VS catch the exceptions etc.

    But anyway, your original question was also very vague. Could you provide more information in future? That way, the information given to you will be more relevant in future.


    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
    Visit my (not very good) blog at
    http://c2kblog.blogspot.com/
    • Marked as answer by strathmore500 Saturday, May 15, 2010 3:37 PM
    Saturday, May 15, 2010 1:25 PM

All replies

  • Well, you could try removing it and seeing what will happen.

    But quite simply it is there for the convenience of not having the console window automatically close after your program has finished.

    For example, if you have the program

    #include <iostream>
    using namespace std;
    
    int main()
    {
     cout << 21;
     return 0;
    }

    What would happen is the program would print 21 to the screen and then immediately close not giving you the chance to read what was printed. Of course, this is problematic if you want to see the results. So system("pause"); is there to give you a chance to read what was actually printed. Now if you modify the above code to include system("pause"); we get

    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    int main()
    {
     cout << 21 << endl;
     system("pause");
     return 0;
    }

    What you then get then is the output printed, followed by text saying "Press any key to continue . . ." and it will wait until you press anykey before it closes.

    So it isn't vital, it is just handy when you are writing programs in VC so you can see what is being written before the application closes.

    *Edit*

    Noticed after I posted this.

    Don't post the same question in multiple forums. Just stick to one.

    The post in VC General

    http://social.msdn.microsoft.com/Forums/en-GB/vcgeneral/thread/b588416b-2bc3-429f-979c-73a12e0f04e6

     


    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
    Visit my (not very good) blog at
    http://c2kblog.blogspot.com/
    Thursday, May 13, 2010 8:28 PM
  • Thanks, but I do know all that. It does'nt answer my question since Visual C++Express has the pause feature built in. My system is obviously not working properly, so I will reinstall rather than attempt to get solutions to fix it from the forum.
    Saturday, May 15, 2010 12:53 PM
  • No, your system is working properly. It only pauses if you start your application without debugging. (Press Ctrl+F5 instead of F5, or go to Debug->Start Without Debugging). When the debugger is attached it doesn't automatically pause.

    But by doing this you won't have the VS debugger attached so you won't be able to break into your application or have VS catch the exceptions etc.

    But anyway, your original question was also very vague. Could you provide more information in future? That way, the information given to you will be more relevant in future.


    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
    Visit my (not very good) blog at
    http://c2kblog.blogspot.com/
    • Marked as answer by strathmore500 Saturday, May 15, 2010 3:37 PM
    Saturday, May 15, 2010 1:25 PM
  • Thanks, that's a much better answer this time.
    Saturday, May 15, 2010 3:35 PM
  • the best way to work with the cmd shell is to simply run one and execute the code from there. you can also make a shortcut for it (may have to dismantle it carefully so as not to delete the original file), c:\windows\system32\cmd.exe and set the working directory to your project path.

    [windows-logo-flag-key]-R cmd [Enter]

    then cd around into the path of your project. I usually just make batch files to do this,or use short paths from root.

    when you want to execute your code, simply type the name of your executable and hit [Enter].

    from there you can also do things like

    start notepad myfile.cpp

    hope this helps

    Tuesday, January 08, 2013 11:30 PM
  • Hi stratmore500,

    I personally had no problem with VS2010 or VS2008 ever but since you seem to have a Problem with it i suggest you to change the run the Program without Debugging. I am pretty sure that VS has a pauser.

    Suggestion:

    Using System("Pause") is a bad Programming Practice because System() function is OS dependent and also can be easily damaged considering if a Malware or a Virus damages any of the System Commands.  Try to use getchar() or cin.get() instead of System().

    Sunday, January 20, 2013 8:13 PM
  • Hi stratmore500,

    I personally had no problem with VS2010 or VS2008 ever but since you seem to have a Problem with it i suggest you to change the run the Program without Debugging.

    @Cyberwarfare1 -

    Just a "heads up" in case you hadn't noticed:

    The original post in this thread from stratmore500 was
    made more than two and a half years ago. Hopefully,
    that poster isn't still waiting for an answer. ;-}

    Adding additional helpful material to an old thread
    is fine, but addressing comments to someone's post
    made long ago may not be worth the time and effort
    you expend. Keep an eye on the date/time stamps.

    - Wayne

    Sunday, January 20, 2013 9:29 PM
  • Hi,

    I totally forgot to look at the Time sorry hopefully i will not commit the same mistake.

    Regards,

    Sunday, January 20, 2013 10:02 PM