0

I'm currently writing a very simple program for a class, and am quickly realizing that I never really knew what the System("pause") command was doing.

The prompt for the program demands that I have the Command Arguments (located in Debug->Properties->Configuration Properties->Debugging in Visual Studio 2013) set to an input file so that I can read from the file using "cin". After setting this property, however, I've noticed that the familiar System("pause") command no longer works to keep the console window open. (I changed the property back to its default and it worked again, so this seems to be the problem.)

Any explanations? I'm really curious as to what System("pause") is really doing in the background now.

AustinC
  • 169
  • 2
  • 7
  • Is your program interactive or isn't it? If it's interactive, how can it work with this kind of redirection? if it's not, why is it waiting for a key to be hit? It sounds like you are intentionally breaking your program to allow it to do what you've inexplicably configured your IDE *not* to do and with no consideration for sound design principles. (Many classes, sadly, teach bad habits. The is a common one.) – David Schwartz Jan 18 '17 at 02:40
  • 2
    You are not supposed to use `system("pause")` to keep your window open. It is always a sign of garbage-quality code. If you are running under Visual Studio IDE, set "Subsystem" in project properties to "CONSOLE" and the window will stay open by itself (when running without debugger). – AnT stands with Russia Jan 18 '17 at 02:41
  • @DavidSchwartz This command is simply being used to pause the program for debugging purposes, so that I could see the output of the program. I've since read the link you provided below in response to another answer. I was able to get around this by using breakpoints. – AustinC Jan 18 '17 at 02:45
  • @AustinC If you want the window to stay open, why not tell the thing that opened it, and the thing that's closing it, not to close it? (Were you taught to use `system("pause")`? if so, honestly, find a different instructor if you possibly can. That is a *huge* warning sign that the instructor fundamentally doesn't understand the role of a C or C++ program or how to think about designing a program to fit a sensible scope.) – David Schwartz Jan 18 '17 at 03:01
  • @DavidSchwartz I was taught to use it for debugging, yes. I would love to know what other way is recommended, as I'm not following you in your suggestion. What are you saying is opening and closing it, and how would I go about manipulating these processes? – AustinC Jan 19 '17 at 06:40
  • @AustinC It depends what IDE you're using, but presumably it supports developing console applications or you shouldn't be using it to develop console applications. When it runs a console application, it should be creating and managing its console and you should be able to tell it how you want that done. – David Schwartz Jan 19 '17 at 17:02

1 Answers1

1

The system function on Windows launches an instance of cmd.exe and asks it to execute the command you pass, which is pause in this case. Normally cmd.exe would wait for the user to press a key. In other words, it reads one character from the standard input device and returns immediately once it reads it. Well, you gave it a file as the standard input device, so it reads one character from that file and returns immediately.

It's doing exactly what you asked it to do. There's nothing mysterious going on in the background.

You can demonstrate this easily by just entering pause < foo at the command line, where foo is any file. You'll see that pause returns immediately in that case.

And as David Schwartz points out in comments, using system("pause") is a horrible practice. If you need to wait for user input to continue, do it yourself with a simple pair of cout and cin calls.

Community
  • 1
  • 1
Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
  • That's interesting. How would I go about replicating the effect of System("pause") in this scenario, you think? Or is it possible? – AustinC Jan 18 '17 at 02:32
  • Sure, why use `system` at all? Just write a message to `cout` telling the user to press enter and then read from `cin`. – Carey Gregory Jan 18 '17 at 02:33
  • 2
    You really just shouldn't. Using `system("pause")` is [horrible practice](http://stackoverflow.com/a/1107717/721269). – David Schwartz Jan 18 '17 at 02:34
  • @AustinC I agree with David Schwartz. – Carey Gregory Jan 18 '17 at 02:35
  • @CareyGregory I agree. I was using this command for debugging purposes only. I've read the link provided by David Schwartz and decided to try the breakpoint solution to this issue, and it worked. Unfortunately, the cin and cout trick would not work, since cin seems to be just reading from the file and going on its merry way. In any case, thank you both for your helpful answers. – AustinC Jan 18 '17 at 02:48