0

I'm having trouble getting a simple C++ script to run in VSCode (I'm new to both). I followed these instructions, and the status bar displays "C++" on the bottom right of the screen, next to the smiley face. I then ran the following script:

#include <iostream>

using namespace std;

main()
{
    cout << "Hello World!\n";
    return 0;
}

When I run it, the script's path flashes on the output screen and disappears. I expected it to display "Hello World" in the output.

I can run the script from the command window (I'm in Ubuntu) and the output file behaves as expected when executed.

Community
  • 1
  • 1
Jon
  • 1,189
  • 11
  • 17
  • Possible [duplicate](http://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio). – Mohamad Elghawi Feb 12 '16 at 22:08
  • Does this answer your question? https://stackoverflow.com/questions/32645271/how-do-i-get-visual-studio-code-to-pause-on-exit-when-debugging/73369616#73369616 – starball Aug 16 '22 at 06:39

1 Answers1

0

Your program prints message to STDOUT and exits. Add some kind of wait (you can read STDIN for example) if you want to see its output.

PS: Why do you call your program "script"?

Paul
  • 13,042
  • 3
  • 41
  • 59
  • I added cin.ignore(); before return 0; but I get the same behavior. When I run it now in the command window, it waits for me to hit enter as expected. – Jon Feb 12 '16 at 22:49
  • I think you could argue that this is a script, as it just runs start to finish, straight through :) See https://en.wikipedia.org/wiki/Scripting_language, paragraphs 2-3 in the 'Characteristics' section. – Jon Feb 12 '16 at 22:50
  • Can you show the script you added as a task? Script from the SO post you mentioned only builds the file, it doesn't run the compiled program. – Paul Feb 12 '16 at 23:57
  • Ah, that might be what I'm getting confused here. I'm not sure what you mean by "script you added as a task" - all I've done is hit Ctrl+Shift+B on the script I showed in my post. How do I run it after it builds? Can I run it within VSCode? – Jon Feb 13 '16 at 16:12
  • You referenced the post, where the answer suggests pasting some code into `tasks.json`. If you pasted that code as is, your `tasks.json` only builds your sources by invoking `make all` in current directory. I'm not sure VSCode can debug C++ code. I'd suggest you try QtCreator, Eclipse+CDT, KDevelop or any other IDE designed to develop and debug C++ code. – Paul Feb 13 '16 at 18:46