2

I just started learning c++ and while reading the c++ tutorial in http://www.dev-hq.net/c++/1--the-basics i saw that writing a c++ program in some text editor is different from writing one in visual c++. It said something like "Visual C++ will require some sort of application pausing before the end of the 'main' function in most cases as it generates a window which will disappear after execution". what does this mean?

kaleidoscope
  • 61
  • 1
  • 4
  • if you're debugging, you could just set a breakpoint before main ends to see the console window (that's where your standard output and error messages go to by default) – Raja Oct 18 '13 at 06:15
  • 2
    Wow, this "issue" has been present in VS since... forever, and instead of MS adding an option to keep the console window open, people are still finding new workarounds... – rubenvb Oct 18 '13 at 08:17
  • @rubenvb VC++ has been keeping the console open since at least version 6, i.e. 16 *years*! You just have to use "Run Project" instead of "Debug Project" to launch. – Sebastian Redl Oct 18 '13 at 13:35
  • @Sebastian you have my upvote. I am mistaken. – rubenvb Oct 18 '13 at 13:42

4 Answers4

5

Short Answer: It isn't mandatory. The site suggests so that the output in the console window can be seen before the program exits (thereby closing the window).

Long Answer: If you're using an IDE to develop a C++ or C program with no GUI, the output is shown by the IDE by launching the console/terminal window (also called the command prompt). This window is shown as your program is started and closed immediately when your program ends. Say if you just print "Hello, world!" and don't give any pause, the terminal window would open, show Hello, world! and immediately close, all in a blink of an eye. So it's customary that a pause is inserted in the end. This problem isn't seen on all IDEs; some show the output in its own integral window, some insert the pause behaviour through script, etc.

It is to be understood that this is just a hack to make the window stay and making a blocking call to do so is useless from the program's viewpoint. Also don't use system("pause"); as it has issues, apart from the fact that it's not portable and works only on Windows platforms.

#include <iostream>
#include <limits>

int main()
{
    std::cout << "Press ENTER(s) to exit...";
    // flush data in cin's buffer
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    // blocking call to fresh input from user
    std::cin.get();
}
  • This would be a portable way i.e. irrespective of the platform you're using it will work.

  • An alternative is to use getchar (not the similarly named getch) which is portable as mandated by ISO C Standard but it has its quirks.

  • Another way would be to put a breakpoint as suggested by Raja. It'll also get your feet wet in learning the debugger, an essential tool in programming.

  • To avoid the issue altogether run the program yourself from the console window. Although sometimes it may get a bit tedious for a beginner, hand compiling code and running it yourself without an IDE gives you more insight and is a good learning experience.

I'd still recommend hand compiling or the debug breakpoint methods instead of injecting a fake pause/blocking call in the program; notice that the input received from the user is just ignored and never used in the program further down the line and thus is redundant from the program's standpoint, hence the recommendation to not use it.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
  • How do i do it ? Am just a noob in this .. When i run a simple hello world win console, window gets closed before i can even read it. – kaleidoscope Oct 18 '13 at 06:15
  • Thank you, now i understood what they meant. – kaleidoscope Oct 18 '13 at 06:38
  • Please, don't generalize to any IDE, the problem is specific to those on Windows (and even there it would be possible to hack around it, but I don't think anybody did). On other platforms the output is easier to capture, so the the IDEs there do and show the output in some "output" tab or embedded "console" tab. On windows it's not done because it takes ugly hack to do it fully generally. – Jan Hudec Oct 18 '13 at 07:55
  • @JanHudec: Fixed now with more detail. Thanks! – legends2k Oct 18 '13 at 08:12
  • @legends2k See my own answer. All the answers here assume that the assertion made in the question ("Visual C++ will not keep your console window open") is correct, when it isn't. Your answer gives quite a few workarounds, but it fails to point out that the particular problem doesn't exist in the first place. This means that the guy who asked, as well as all other people who read this thread in the future, will keep adding these snippets to their code for no good reason. – Sebastian Redl Oct 21 '13 at 12:39
  • @SebastianRedl: I myself have nothing against VC++, in fact I used it daily. Besides you missed the point, the question was not about the IDE (although the title seems to) but about a site where a tutorial says about pause (description says this). – legends2k Oct 21 '13 at 17:10
1

When you use "Run Project" (Ctrl+F5 in the classic Visual-C++ key binding, not just F5, which is "Debug Project"), Visual Studio will keep your console window open until you press a button.

It's only when you use "Debug Project" that it doesn't. This makes sense, because you generally only want to use that option (which has a much longer startup and shutdown time and runs slower) when you're actually hunting a problem, in which case you probably have breakpoints set in your program and can simply set one on the last line of main().

So all the proposed workarounds, and the flames about Visual Studio, are simply unnecessary and wrong.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • They are not unnecessary, depending on what you are trying to accomplish. Visual Studio keeping the console window open when you Run Project is nice, but what about when you distribute the binary that performs some calculation and don't want the console to shut down automatically when you run it? – patriot Oct 18 '13 at 22:18
  • @Sean There may well be situations where you want to keep the console window open. But the question is about the assertion that Visual C++ doesn't do that by itself, not about any other use cases. – Sebastian Redl Oct 21 '13 at 12:41
-1

It just means that the executable window will close as soon as it's done executing.

I would recommend using a simple getchar(); statement from the standard library (<iostream> or <stdio.h> header) before you return 0;. This means you will have to press a key for the window to close. As you mention Visual C++, system("pause"); will work as well before the return on Windows systems.

Of course, if you return from main earlier (common with error codes, unhandled exception, or an exit call somewhere), you likely won't get to that pausing mechanism. As you are just starting out, it is unlikely you will run into that stuff for now though.

Running your program from the command line will work as well to see the output.

patriot
  • 107
  • 5
-1

Here is something I use to pause my console applications and let me see the results:

void waitForUser()
{
    // if for some reason cin is in invalid state
    // we need to clear it
    std::cin.clear(); 

    // if we used cin to input something it is likely to
    // contain trailing \n symbols , we need to get rid of them
    std::cin.ignore( std::cin.rdbuf()->in_avail() );

    // we wait for a user to hit Enter here                     
    std::cin.get();
}

Just put a call to this function at the end of main

Terenty Rezman
  • 357
  • 2
  • 12