Running exe in VSCode produces no output - c++

I want to compile C++ in VSCode. I followed the walkthrough here
When I run the example program in VSCode terminal, nothing is output into the terminal. I tried in powershell and in cmd using the dropdown in VSCode terminal window.
When I run the .exe from a standalone cmd terminal, it works fine. Could the problem be something about not knowing how to write the output? I'm guessing the program runs but just doesn't know where to output.

I opened VS code before adding mingw to my PATH. I simply had to restart VS code. I really figured each time I opened a new terminal in VS Code, it would.. open a new terminal. Perhaps they do some kind of virtual terminal and it's really all one terminal from launch? Who knows. Either way, restart VS code.

Command Prompt method :
first changer the directory to where file is located.
g++ -o program Menu-driven-program-DAA.cpp&program.exe
Running Cpp program through powershell terminal in vscode:
g++ -o program Menu-driven-program-DAA.cpp
.\program.exe
I've mentioned both the commands in vscode powershell terminal seperately because it throws an error
Note:- try gcc instead of g++ if it doesn't works for me only g++ did.

I encountered the same problem and by pure chance following worked in my case: When I opened VSC from a GIT shell via 'code .', I didn't get any output from a shell within VSC. But when I opened VSC from windows command prompt via 'code .' and executed the same file again and it worked.

Related

how do I run my code on cmd instead of vscode's internal terminal

everyone. I'm kind of new in this field. So bear with it. I'll try to be as specific as I can:
let's say when I run a code(c++ file) in VScode it runs that code on VScode's internal terminal..like this => VScode
but I want that code to run on my Window's CMD like "CodeBlocks" software. Like this => CodeBlocks
but I don't know how to do it in VScode. I mean, when I click on 'run' button it should execute that code on CMD. I tried many ways but it's not working. Help please and thanks in advance.
VSCode has a built-in terminal. That is why in the first case(first image in your question) you see the output as it is. If you don't want to use the built in terminal provided by VSCode then i suggest you open a standalone/separate terminal. And then cd into the project you want to build/compile and then compile the program from there.
Basically, open a terminal externally then go(cd) to your workspace folder and finally compile and run in the external terminal.

How to run a program from VSCode in Windows CMD terminal? [duplicate]

I have a Console application open in VS Code. When I press Ctrl-F5, the output of my program is displayed in a DEBUG CONSOLE window, along with other text.
How do I get Visual Studio code to launch my program in a new console window?
As documented here this can be achieved using this setting:
"console": "externalTerminal"
The settings file is in the solution directory: .vscode/launch.json .
When you're just using Tasks and not Launchers, you can follow the advice here.
For me on Linux, I changed my shell command in VSCode from command to gnome-terminal -e command. That did the trick; that's all I had to do.
Note that if you do this you can get rid of the presentation option set from your task.

mingw32: all compiled executables hang

I've just installed the most recent version of mingw32 and trying to compile even a hello world c++ program fails.
Installation was in the default C:/MinGW directory. I chose the option to install with mingw-get gui, and I selected the Basic Setup tab on the left-hand column, marked the mingw32-base and mingw32-gcc-g++ options for installation. Then I applied changes and downloaded all the files. Lastly I added C:/MinGW/bin to the path of windows.
Now I create a file that looks like this:
test.cpp:
#include <iostream>
int main() {
std::cout << "hello world!" << std::endl;
return 0;
}
and then I compile with g++ test.cpp
Now, the problem is a bit harder to diagnose. When I execute the output a.exe file, the command line simply hangs, no output, no return. If I try and cancel execution with ctrl-c, nothing happens. When I forcefully exit out of the command line, and try to delete a.exe, windows tells me the executable is currently opened by system, and the only way to forcefully remove the file is with FileASSASSIN.
Some notes, I have visual studio 12 installed, but nothing pertaining to it in the path. I had a previous installation of cygwin64 but it has been removed entirely. I get the same behavior running it from powershell, normal command line, or Console2. Google suggested I try statically linking libstdc++ with -static-libstdc++ to no avail.
Also, when compiled with debugging symbols and then ran with gdb a.exe, and then you run start, it also hangs just the same.
I'm out of ideas on getting information for troubleshooting here. Does anyone have any insight?
On the suggestion of #M.M, I discovered that avast antivirus was the silent cause of halting any executables made by mingw32. This was puzzling, because I was running executables made by visual studio on command line only a few hours ago.
The lesson: If you encounter unexpected behavior, always try your antivirus.

running unix like command "./a.out <data.txt" in windows environment on c++ file

How do I run a command like interface on windows and use the g++ and ./a.out
I am a beginning programmer used to using putty/ssh to write (nano), compile (g++ command), and run (./a.out) c++ programs.
Our class has now switched to netbeans, but our latest assignment requires us to use the ./a.out <datafile.txt-like command.
Or is the input redirection style ./a.out <data.txt unique to unix and cannot be done in windows?
edit: the < input redirection marks made my post mostly unreadable. Sorry about that
2nd edit: There is actually a terminal built into netbeans that VERY conveniently starts in your project directory. open it in netbeans by selecting Window -> output -> Terminal
Cygwin is a collection of tools which provide a Linux look and feel environment for Windows.
start,
run,
cmd,
cd directory,
g++ a.cpp,
a.exe
Windows includes . in $PATH, so the ./ at the beginning is superfluous (and wrong, since it would be .\). The rest is the same.

Can't run programs on my mac

I've been programming on windows for about three months now and when my computer finally died I bought a mac. I'm really happy with it except I can't figure out how to run my c++ programs.
On windows it would open up in command prompt so I figured it would do the same thing except with terminal.
After I write my programs (in Xcode) I can compile them and it'll tell me if there are syntax errors but the run and run related buttons are all Grey and unusable.
It's incredibly annoying that I can't see how the program that I put 4 hours into runs and even more annoying that I don't know if I've made a runtime error. Someone please help.
Thank you very much, James
Assuming that you're trying to write a program that you interact with on the command line, you want to create a "Command Line Tool" project, found under "Application" in the Mac OS X section of the New Project dialog. Choose "C++ stdc++" from the "Type" dropdown, as well. This will give you a main.cpp that should look familiar to you, with a "Hello, World!" sample.
Note that when you Run this program, it might appear to do nothing - You need to open the Console (from the Run menu, or shift+command+R) to see your output.
Xcode deals in projects. If you're just opening the source file, there is no project. Create a project with the files in it and Xcode should let you run it.
Alternately, you can just use G++ on the command line to compile your files and run the resulting executable there.
Try compiling from the terminal as stated above. The a.out file should be created, and should run as long as Xcode has been installed. It should work fine from the terminal, and point out any errors in the compiler's output.
To runs the a.out file, ensure you are in the files directory in terminal and type ./a.out