Beginner's use of system("pause");
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Beginner's use of system("pause");

  1. #1
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Thumbs down Beginner's use of system("pause");

    As long as I've been around here, I've seen lots of beginners use

    Code:
      system("pause");
    statements to have their toy console apps to rest before they exit.

    Yes, of course they need some way of doing some kind of "Press any key to continue..." when running their program from an IDE, but is it really required to employ an entire command shell for that trivial task? There are really more efficient ways of doing that.

    The reason why I have started a thread about that marginal topic at all is: This way of doing that is so common that I started to ask myself whether there might be some common source that teaches them to do so.

    Ok, maybe I'm only nit-picking because I have started learing programming at a time when every CPU clock tick and memory byte was precious, but the notoriousness of that habit just made me curious...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,317

    Re: Beginner's use of system("pause");

    Because when you teach a student the "hello world" program under windows, and you want to make sure the shell doesn't close so they can see the window, what are you going do to?
    1 - Tell them to use write system("Pause");
    2 - Tell them to use the windows.h header, to get a handle to the current terminal, and send the pause message via the handle?

    The thing about programs that use system("Pause") is that 99% of the time, they are toy programs anyways, and while portability is always good, most of them are scrapped at the end of the day. What's more, it is the easiest way of pausing the console, and in programming just like business, time is money. system("pause") is a business decision: "Do I want to spend 5 minutes coding something I'll call once?". Besides, when issuing a pause command, the human operator is the only bottleneck when you think about it. The cost to call system(pause) is irrelevant.

    Of course, there is a time and place for everything. I think the system("pause") command is one of those things that people tell you "you shouldn't do that because it is bad", when in reality, it should be more like "You should be aware of what you are doing, and THEN make an educated choice of whether you want to pay that price".

    I write toy programs everyday, and they all feature system("pause").
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,316

    Re: Beginner's use of system("pause");

    Quote Originally Posted by Eri523
    The reason why I have started a thread about that marginal topic at all is: This way of doing that is so common that I started to ask myself whether there might be some common source that teaches them to do so.
    There might have been a single source at one point, but now I believe this has found its way to examples in many different tutorials and books, so there is no common source.

    Quote Originally Posted by monarch_dodra
    Because when you teach a student the "hello world" program under windows, and you want to make sure the shell doesn't close so they can see the window, what are you going do to?
    If I have control over the development environment, I would either provide the student with an IDE that is configured to pause the console window at the end, or have the student open a separate console window. If I only have control over the material, then I might prepare a function for the student to use, e.g.,
    Code:
    void pause()
    {
        using namespace std;
        cout << "Press enter to continue...";
        cin.clear();
        cin.ignore(10000, '\n');
        cin.get();
    }
    with instructions to write pause(); at the end of the global main function should the student face the problem of the console window closing prematurely. An advantage here is that my material would then be applicable even if such a command was not available, or not named pause.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: Beginner's use of system("pause");

    Ok, thank you both for your points. I 99&#37; agree with monarch_dodra and 100% with laserlight. The extra % is for suggesting a common pause() function with less overhead, of course. If the standard guys would ever ask me, I would recommend that for TR2.

    Quote Originally Posted by laserlight View Post
    If I have control over the development environment, I would [...] provide the student with an IDE that is configured to pause the console window at the end [...].
    The funny thing is that VC++ 2010 Express does exactly that for Win32 console apps (if ran without debugging), but not for C++/CLI console apps. For that reason, in the latter I use to write this at the end of main() in a toy program:

    Code:
      Console::WriteLine("Press enter key to continue...");  // Only if I have lots of spare time... :D
      Console::ReadLine();
    (I hope I won't get kicked out if this forum now for admitting that I use C++/CLI... )

    EDIT: Changed "any" to "enter" in the little listing. Of course, "any" doesn't make sense if I do a ReadLine()...
    Last edited by Eri523; October 15th, 2010 at 07:26 AM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,317

    Re: Beginner's use of system("pause");

    Actually, I use a "pause()" function too (for the sake of abstraction), but the body of said function is just system("Pause"). I find it is the best solution as the Windows provided pause is really "Press any key to continue" whereas Laserlight's is more tightly stream bound (and may have some un-intended side-effects on your input stream).

    The day I'll port my program, I'll only have one function to change, Probably to Laserlight's implementation
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: Beginner's use of system("pause");

    Quote Originally Posted by monarch_dodra View Post
    [...] (and may have some un-intended side-effects on your input stream).
    Which side effects should that be besides flushing the stream and resetting the error control state? IMO these effects would be rather intended when doing a pause, and definitely would do no harm if only called at the end of main(), what originally was the only place for calling it I had in mind, but if it's there, people of course may call it any time they want...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,317

    Re: Beginner's use of system("pause");

    Quote Originally Posted by Eri523 View Post
    Which side effects should that be besides flushing the stream and resetting the error control state?
    It's up to debate, but I would say those are side effects yes. IMO, pausing a program and its input are two completely un-related things.

    Consider especially that some command-line programs' input stream are not the keyboard, but the raw output stream of other programs (think LAME.exe, x264.exe etc.). The side effect would be the program not stopping, and the stream being completely corrupted.

    Of course, those aren't toy programs anymore, but even for a toy program, try this:

    Code:
    int main()
    {
        pause();
    }
    This is what my console looks like:
    Code:
    Please press enter...
    hello world!<return>
    What's up?<return>
    -------End program
    The program ask me to press enter, but it first lets me write an entire sentence because it is trying to flush the stream. Then, I get to write another sentence, before the program validates my input. It feels strange, because I expect to just hit space.

    Also, try this:

    Code:
    int main()
    {
        std::string name;
        std::string surname;
        std::string age;
    
        std::cout << "Please type your Name, Surname and age seperated by spaces" << std::endl;
        
        std::cin >> name;
        std::cout << "Your name is " << name << "!" << std::endl;
        pause();
    
        std::cin >> surname;
        std::cout << "Your surname is " << name << "!" << std::endl;
        pause();
    
        std::cin >> age;
        std::cout << "Your age is " << name << "!" << std::endl;
        pause();
    }
    Am I nitpicking? A bit. Yes. But I find the crusades against system("pause") to be a bit silly.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: Beginner's use of system("pause");

    Quote Originally Posted by monarch_dodra View Post
    Consider especially that some command-line programs' input stream are not the keyboard, but the raw output stream of other programs (think LAME.exe, x264.exe etc.). The side effect would be the program not stopping, and the stream being completely corrupted.

    Of course, those aren't toy programs anymore [...]
    Ok, these would be problematic. Actually, I have written a bunch of DOS console programs (in C) in the past that used some kind of pause function. As they would certainly get screwed up when stdout would be redirected (and that was a quite common scenario), I had the pause function check for the redirection and skip the actual pause if there was one. This involved IOCTL calls however, and thus was not portable. I think that's a reasonable effort for a non-toy program.

    [...] but even for a toy program, try this:

    Code:
    int main()
    {
        pause();
    }
    This is what my console looks like:

    Code:
    Please press enter...
    hello world!<return>
    What's up?<return>
    -------End program
    Hmmm... I think that's because the cin.ignore() call in laserlight's pause() implementation uses the '\n' as delimiter. Maybe a more sophisticated pause() could avoid this problem? Unfortunately I can't present one off-hand because of my limited iostream experience. Again, I would consider this to be an acceptable effort if the function is to be reused.

    And yes, your second example gets screwed up too, but isn't that an abuse of the pause() function? I must admit, though, that it gets less screwed up if pause() simply calls system("pause").

    Well, this is becoming a much bigger discussion than I ever imagined...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Beginner's use of system("pause");

    Quote Originally Posted by Eri523 View Post
    As long as I've been around here, I've seen lots of beginners use

    Code:
      system("pause");
    Other points you didn't bring up:

    1) Some joker could write a "pause" program that erases files. I've heard that this has already been done at one university.

    2) If the student is testing the workings of construction and destruction of global objects, "pause" will not work as the pause is done before the closing main() brace is hit. There have been a few threads here where the student is tripped up because they put "pause" in their program.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,317

    Re: Beginner's use of system("pause");

    Yeah, this conversation probably got dragged out a bit.

    As for me abusing the pause function, well yes, yes I am, but the point is that no function should work "As long as you don't call it too much".

    And yes, maybe the implementation of the pause function is not optimal, but I'd be willing to bet you'll never be able to find a stream-based implementation I can't break!

    As a matter of fact, imagine a program that reads from cin and pauses every time it reads "hello". Run that program feeding it a file as input.

    My gripe is that people say "Don't use system("pause"), it's bad" but there is no real portable equivalent. I think the "best" implementation for pause is the one that has a different implementation per system.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Beginner's use of system("pause");

    Quote Originally Posted by monarch_dodra View Post
    My gripe is that people say "Don't use system("pause"), it's bad" but there is no real portable equivalent. I think the "best" implementation for pause is the one that has a different implementation per system.
    There is a CodeGuru article written a couple of years ago that discusses this, and shows a pause() function equivalent that is safer to use. Don't have the URL link handy.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,316

    Re: Beginner's use of system("pause");

    Quote Originally Posted by Eri523
    I must admit, though, that it gets less screwed up if pause() simply calls system("pause").
    In any case, remember, this is code that might be placed in a book to be copied by a reader. It is best if it is simple and portable, and in this case possible for the reader to understand after a few chapters.

    Making this pause function a wrapper for a pause command would simplify it and make it more of a true pause function, but may lose portability (and then there's the malicious pause program problem that Paul McKenzie mentioned, but if your pause program can be harmful, then you have a problem anyway).

    Quote Originally Posted by monarch_dodra
    As for me abusing the pause function, well yes, yes I am, but the point is that no function should work "As long as you don't call it too much".
    Actually, my suggested pause function works best if it is never used at all, i.e., if the program is simply run from a separate console window, which actually makes the most sense and addresses Paul McKenzie's point on "testing the workings of construction and destruction of global objects", or even those objects local to the global main function. So, it is not designed to be used as a general purpose pause function; it is designed as a crutch for beginners who should not get bogged down in the details while they are still in the early stages of learning.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,621

    Re: Beginner's use of system("pause");

    Here is an interesting link on a (related) subject:

    http://www.daniweb.com/forums/thread90228.html
    Last edited by Philip Nicoletti; October 15th, 2010 at 12:29 PM.

  14. #14
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: Beginner's use of system("pause");

    monarch_dodra, I think you're right in that any stream-based pause function would have more or less the same specific weaknesses. To get around this, I think of using the conio functions, but I'm afraid they're most probably specific to Windows or even MS VC++.

    For overcoming the necessity of calling pause() at the end of main() I think of making some kind of reverse-RAII pause class that does the pause on destruction and instantiate it as a global object. But even then there's no way to control whether it would actually get destroyed as the last one of the global objects. I better don't start to think about C++ runtime manipulations... But couldn't it be a surprisingly simple solution to just wrap the toy program into another one that does nothing more than exec...() the toy and then pause? The only problem then would be to have the IDE use that.

    In the end, simply using a separate console window might be the best solution. (Don't remember right now who suggested that.)

    And BTW, at least under Win XP a malicious pause program wouldn't have a chance: The pause command there is hardcoded inside the shell and always has priority over any program with the same name.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  15. #15
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,317

    Re: Beginner's use of system("pause");

    Quote Originally Posted by Eri523 View Post
    monarch_dodra, I think you're right in that any stream-based pause function would have more or less the same specific weaknesses. To get around this, I think of using the conio functions, but I'm afraid they're most probably specific to Windows or even MS VC++.

    For overcoming the necessity of calling pause() at the end of main() I think of making some kind of reverse-RAII pause class that does the pause on destruction and instantiate it as a global object. But even then there's no way to control whether it would actually get destroyed as the last one of the global objects. I better don't start to think about C++ runtime manipulations... But couldn't it be a surprisingly simple solution to just wrap the toy program into another one that does nothing more than exec...() the toy and then pause? The only problem then would be to have the IDE use that.

    In the end, simply using a separate console window might be the best solution. (Don't remember right now who suggested that.)

    And BTW, at least under Win XP a malicious pause program wouldn't have a chance: The pause command there is hardcoded inside the shell and always has priority over any program with the same name.
    I think you're reading too much into it. For starters, your "reverse RAII" method would be even worse because you'd have no control over when exactly it is destroyed, relative to all the other globally destroyed objects. Chances are you'd only see half of the globals destroyed before your pause kicked in, and then you'd miss the other half.

    pauses are usually meant as a very quick and easy way to "debug" or "fix". If you call your program from an already open shell, you really shouldn't even need it.

    If your program requires you to write a very complicated and fancy pause infrastructure, than chances are you are trying to patch a problem that should really have been fixed at the source (here, making sure the console doesn't close at the end).

    That is my 2 cents/conclusion: Use system("pause") when you need a quick pause for development reasons. If you need anything more subtle, rethink your model.

    PS: Most distributed command line programs terminate when they finish running, they don't prompt you to press any key.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center