No output in console from std::cout in Visual Studio 2019 x64 Native Tools Command Prompt - c++

I have some C++ executables which contain various std::cout outputs.
In Visual Studio 2019 x64 Native Tools Command Prompt, when I run these executables, there is no output displayed on the console.
If I redirect the output to a file, the output is indeed printed to the file.
The following program for example does not output anything to the console:
#include <iostream>
int main (int argc, char** argv) {
std::cout << "Hello world" << std::endl;
}
Is there some setting or variable I need to set in order to see the output in the console itself ?

According to the code you provided, my console program can output normally.
I suggest you could verify that the Visual C++ developer command prompt is set up correctly. In the command prompt window, enter cl and verify that the output looks something like this:
For more details about how to compiling a Native C++ Program on the Command Line, I suggest you could refer to the Doc: Walkthrough: Compiling a Native C++ Program on the Command Line

Related

"Command "gnuplot" can not be found" (Sciplot, MS Visual Studio 2022)

I am new to C++ and am trying to build a program which receives data and shall transform and then plot it. For plotting I want to use Sciplot, but when I run the program, the message "The command 'gnuplot' is either spelled wrongly or could not be found." appears. I have installed the newest version of gnuplot (5.4.4), but it seems the program is not able to call it.
Here is the code I use for testing sciplot (example is taken from https://sciplot.github.io/tutorials/):
#include <sciplot/sciplot.hpp>
using namespace sciplot
int main(int argc, char* argv[]
{
Vec x = linspace(0.0, PI, 200);
Plot2D plot;
plot.xlabel("x");
plot.ylabel("y");
plot.xrange(0.0, PI);
plot.yrange(0.0, 1.0);
plot.legend()
.atOutsideBottom()
.displayHorizontal()
.displayExpandWidthBy(2);
plot.drawCurve(x, std::sin(1.0 * x)).label("sin(x)");
plot.drawCurve(x, std::sin(2.0 * x)).label("sin(2x)");
plot.drawCurve(x, std::sin(3.0 * x)).label("sin(3x)");
Figure fig = { {plot} };
Canvas canvas = { {fig} };
canvas.show();
return 0;
}
Do I need to tell Visual Studio the path to gnuplot? Or did I miss something else?
You need to tick the checkmark "Add application directory to your PATH environment variable" on the sixth page of the gnuplot installer. Alternativly add the folder of your gnuplot.exe to your PATH environment variable.
If you use MS Visual Studio for compiling the example, you need to close and re-open MS Visual Studio. MS VS does only "read" environment variables at program start-up.
This sciplot project calls std::system with gnuplot, so your cmd has to recognize the gnuplot command/find the gnuplot exe.
I tested it, and it works like I described.

Notepad++ NppExec console warning, need explanation "C++"

I've tried using Notepad++ to code c++ and followed a few tutorials on youtube, here's what I did:
-Installed gcc/g++ compiler using mingw64
-Installed NppExec plugin on N++
-Typed in the following compilier script and saved as C++:
NPP_SAVE cd $(CURRENT_DIRECTORY) g++ $(FILE_NAME) cmd /c $(CURRENT_DIRECTORY)\program.exe
Anyways whenever compiling a program, for example a simple program
#include <iostream>
using namespace std;
int main(){
cout << "Online\n";
system("pause"); //So that cmd doesn't disappear immeadiately on running.
return 0;
}
The console displays the following warning:
"C:\Users\pc\Desktop\Courses\Projects\C\program.exe' is not recognized as an internal or external command, operable program or batch file."
My question is, When I run the program on cmd, it runs perfectly but the error displayed during linking says that the folder does not exist in %PATH%
Any explanation?
Thank you!
Ok so, what I basically did was change the script,
cmd /c $(CURRENT_DIRECTORY)\program.exe
To be later
cmd /c $(CURRENT_DIRECTORY)\a.exe
the console worked fine and even received input
Here is a link to a similar problem:
How to compile/execute C++ code from within Notepad++

My terminal in VS Code won't print anything or give me an error. Don't know why it is not printing

Just installed Visual Studio Code with C/C++ IntelliSense... (Microsoft) and Code Runner extensions. I am also using MinGW. This code refuses to print to my terminal.
#include<stdio.h>
int main(){
printf("Hello World");
}
While viewing this file press Ctrl+Shift+P and type Run Code and press enter.
You should see a terminal opened with OUTPUT tab and see the result or an error.
If an error occurs publish it alongside with your question.

No output on std::cout while outside of IDE

I have this well known c++ program in my Visual Studio 2015 Prof:
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "Hello World!" << std::endl;
return 0;
}
As expected, it shows "Hello World!" if I hit Ctrl+F5. However, if I go to the directory within an cmd.exe and execute the HelloWorld.exe file, it doesn't show anything as output, but does quit (I can type in again).
According to a similar question I checked the settings of the project but I did not need to change anything, the Configuration Properties -> Linker -> System -> SubSystem already is at Console (/SUBSYSTEM:CONSOLE).
Flushing the std::cout also didn't help anything. It is reproducable on every freshly created project of VS2015 on my Win 7 64-bit machine and seems to be presistent after rebooting.
What is wrong with my IDE / settings?
After the hint of #Scheff I threw the exe file into "Dependency Walker". It gave me a missing UCRTBASED.DLL and lots of second and third level missing dlls (I think this is usually the case on every application?).
I somehow think my (recently installed) anti virus did interfere with that, because it shows some messages in the log regarding my HelloWorld.exe.
However, deinstalling anti virus and restarting machine did the trick. I can finally see Hello World! on cmd.exe.

can't debug small program on eclipse helios cdt using mingw/gdb under windows, console freezes

I've been trying to use Eclipse CDT to do some c++ examples, i can run them just fine with the run command, but whenever i try to Debug, the console window freezes up, I'm able to input, but the program doesn't continue.
When I debug, i get the following output on the console window (no breakpoints, but breaks on main because of default settings):
Hello, world
put your name: 15^running
The continue button is disabled and doesn't do anything when I input something and hit enter. The 15 is a random number, sometimes its 16, 20 etc.
If I run the program under eclipse I get the input prompt just fine:
Hello, world
put your name: test
Hello test
this is the code I try to debug:
#include <iostream>
#include <string>
int main() {
std::cout << "Hello, world" << std::endl;
std::string name;
std::cout << "put your name: ";
std::cin >> name;
std::cout << "Hello " + name << std::endl;
return 0;
}
My path var:
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM;C:\Program Files\Java\jdk1.6.0_14\bin;C:\MinGW\bin
Eclipse version: Helios Service Release 2
CDT version: 7.0.2
OS: windows xp
GDB version: GNU gdb (GDB) 7.2
How can I debug this small example under CDT, without issues?
15^running looks as a result record from gdb's Machine Interface. Normally it shouldn't appear in the Eclipse console.
I recommend trying a different Create Process Launcher. It can be changed in the following way:
In Main menu choose "Run" -> "Debug Configurations...".
In the opened "Debug Configurations" window shown below click "Select other..." opposite "Using GDB (DSF) Create Process Launcher".
In the opened "Select Preferred Launcher" window shown below check "Use configuration specific settings", select "Standard Create Process Launcher" in the list below and click OK.
Now go to the Debugger tab in the "Debug Configurations" window, select debugger, e.g. "MinGW gdb" and click Apply.
With the Standard Create Process Launcher I am able to debug your program although "put your name:" is printed only after I type something and hit Enter, because the output stream is not flushed.
Try having a look at this http://www.cprogramming.com/gdbtutorial.html and see if that helps. Like can you press CTRL-C to break?
Also Cannot enter input with gdb. Help! might be a pointer although related to apple.
the following is from the Eclipse website's FAQ:
http://wiki.eclipse.org/CDT/User/FAQ#Eclipse_console_does_not_show_output_on_Windows
Eclipse console does not show output on Windows In Eclipse CDT on
Windows, standard output of the program being run or debugged is fully
buffered, because it is not connected to a Windwos console, but to a
pipe. See bug 173732 for more details. Either add fflush calls after
every printf or add the following lines in the start of the main
function:
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
Seems like an expected bug on windows.