-4

I just started using Visual Studio to write C++ code on Windows 10 and I am using the console application template. My problem is that the console disappears immediately after showing the output of my program even when I simply run a "hello world" example. I tried all kinds of tricks to implement delay but no success.

Walter Tross
  • 12,237
  • 2
  • 40
  • 64

3 Answers3

2

I usually put a std::cin line just before main returns. This will cause it to wait for input before continuing.

Rich
  • 4,572
  • 3
  • 25
  • 31
1

You can do the following( assuming that you are not waiting for user input, in that case you can just follow Rich's answer ) :

  1. Run it in debugging with a breakpoint on the last line before the main returns.
  2. Use a system("pause") at the end before the main returns.( suggesting this just because it's just a hello world program in Visual Studio )
Vishaal Shankar
  • 1,648
  • 14
  • 26
0

A great way to pause the console in Visual Studios for your purposes is to use system("pause"); Though its not portable to other OS and some anti-virus systems don't like it. For your purposes it should work well and is easy to see what the line does.

Hope this helps.

Jake Freeman
  • 1,700
  • 1
  • 8
  • 15
  • 2
    Re: https://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong. Better to not encourage bad practices from the start. – Arnav Borborah Feb 12 '18 at 16:20
  • @ArnavBorborah we are using Visual Studios in the tag. `system("pause");` is a perfectly valid way to do it. – Jake Freeman Feb 12 '18 at 16:21
  • @ArnavBorborah I added a disclaimer – Jake Freeman Feb 12 '18 at 16:24
  • `system` on Windows is unreliable in general because MSVC doesn't use CMD's `/d` option to skip AutoRun commands. Problems related to AutoRun commands can take a long time to solve, since these commands are often set by other programs without consent or advisement. If you're running a simple command, invest the extra effort to call it via `_wspawnl` instead. If you want the console to remain open, simply spawn a shell using the `/d` option. There's no need to run a command in the shell and wait. – Eryk Sun Feb 12 '18 at 17:27