Can't print to console in c++ eclipse kepler - c++

Running this code on windows using eclipse kepler
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
I've built it and run it with no errors, but I get nothing on the console. I've tried flushing the stream as well and that did not change anything.

Probably, this is because Eclipse does not add PATH to MINGW\bin (if you use MinGW, of course). In the "Environment" tag, press "New", set it as:
"Name:PATH"
"Value:C:\MinGW\bin"
I have not enough info to be sure it's your answer, but try.

Related

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.

ofstream outputs in debug but not run

I have a very simple C++ program with a very simple project setup, but when I run the program I get no output. If I run the program in debug mode, it works perfectly. I am using Eclipse Kepler CDT 32 bit on windows with MinGW. I am somewhat new to eclipse, so it's probably something I did wrong.
The program is:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
ofstream outfile("testdata.txt");
int main()
{
outfile << "Program Start\n";
cout << "Program Start\n";
return 0;
}
Help!
If the problem is that the program quickly opens and then closes before you can see the output on the screen, then you can just run your program from any shell (CMD on Windows, bash on Linux, etc.). That way, it won't exit once your program ends and you can see the results.
Make sure also that you flush/close your ofstream before your program exits.
The problem is rather not releted to c++ itself. You should check if via "cmd" typying it in "launch menu" after you click start. Find the path of your program, then run it.
For the very beginning it is recommended to spend a few hours with terminal(cmd). To know how things works. After that you will be independent - you will be able to write the code in any IDE. Also simple trick to make it working is to use std::cin.get() . It is prefered to system("pause").
You open the testdata.txt file using the relative path.
And the created file may be created in the project binary output path, where the executable located in.
You can use everything software to check whether a file is created and its created path.
everything
For example, you can type your output file name testdata.txt into everything software to see where output file created.And check if the testdata.txt created in a wrong path or directory.

C++ on windows closes a program immediately after launching

I installed minGW and the eclipse CDT, and the console keeps doing something weird. The code of the program is
using namespace std;
#include <iostream>
int main() {
cout << "Hello, windows (8, c++)" << endl;
//system("PAUSE");
return 0;
}
You all know it, its the Hello World program. Now when I run this the Eclipse console displays some stuff about building, and then goes blank. And when I navigate to the HelloWorldProgram.exe in the explorer and run it, a windows flashes up and displays "hello world", but then immediately closes. When I do this on Mac OSX there's no problem, and the windows stays up until I decide to close it. Now I know there's a command
system("PAUSE") //I dont know what I need to import to use this. Could you tell me that too?
Which will give me more or less the same effect, but I'd like to know why Windows does it differently from OSX, and what I can do to fix it (bc this annoys the crap out of me).
Looking forward to your replies!
This happens on Windows because this is just the behavior of the Windows console. You'll have to open up the console manually and then running your program through the console you've opened if you don't want the window to close automatically once the program has executed.
You may want to take a look at these:
What is the Best Practice for Combating the Console Closing Issue?
https://superuser.com/questions/186562/how-can-i-keep-the-terminal-open
Don't use system("pause"), it's wrong for a multitude of reasons (read more about it here).
Put cin.get() before return and the window will stay open until you press enter.
If you want to just run your console program, you should open a console, and run it.
Apparently, the OSX version of Eclipse is configured to open a console, and run the program, and not close it. Maybe you can configure the Win version so, too.
You shouldn't meddle with your program to behave differently on another platform, instead wrap it into something that 'adapts' the behaviour.
Probably, you can tell eclipse to use "cmd /c 'yourprogram.exe && pause'", to open a command window and have it execute your program and then pause.
Just add getch(); before return, and add #include <conio.h>.

C++ HelloWorld not printing correctly

I just installed c/c++ development tools for my eclipse and everything is working except no text is being printed in the console when I run the hello world program, but I receive no errors. I'm really stumped, anyone know why this is?
Edit:
Ok I realized if that debug it, it works correctly, but not if I run it, any ideas there?
Are you using a 64-bit version of Eclipse? If so, that might be your problem. The 64-bit version doesn't do console output. sigh Try downgrading to the 32-bit version.
On SO, check this question.
On the Eclipse forums, check this thread.
Does a window pop up then disappear? It could be printing it in console then closing as soon as it hits the end of the code...
try to make your code like this:
#include <iostream>
#include <conio.h>
using namespase std;
int main()
{
cout << "helllo, world" << endl;
getch();
return 0;
}
You must set the environment so the eclipse can find the c++ compiler.
Go to Computer and right click Properties -> advanced system settings -> enviroment variables.
Scroll down in system variables and find the path (it is named so). Press edit and append in the path the value C:\MinGW\bin;C:\MinGW\msys\1.0\bin;. You will have something like C:\MinGW\bin;C:\MinGW\msys\1.0\bin;C:\programfiles........
Then start again the eclipse the problem should have been solved.

Eclipse C++ Running/Debugging problems with console IO

I've been trying to get into C++ programming with Eclipse, but I'm having problems setting up Eclipse.
I have MinGW installed and in the environment path, and I created the simple C++ project with the following source code:
#include <iostream>
int main(void)
{
std::cout << "what is your name? ";
std::string name;
std::cin >> name;
std::cout << "Hello, " << name << std::endl;
std::cin.ignore();
return 0;
}
After successfully building the project (both debug and release configurations), I click the run button and there's no output. The program terminates immediately. I've also tried running in debug mode, but then it will wait for me to type in a name, then display all the output. example console:
scott
what is your name? Hello, scott
I've tried this with both the 32 and 64 bit Windows versions of Eclipse Helios and both versions have the same behavior. Does anyone know what is going on and how to fix this?
I've run the program from the command-line and it works as intended.
edit: After some tinkering around, I found that by copying the MinGW dll's into the same folder as the executable the program will run in Eclipse just fine. Is there an alternative method to have Windows find the dll's in MinGW's bin folder rather than have to copy them over each time?
Is there an alternative method to have Windows find the dll's in MinGW's bin folder rather than have to copy them over each time?
Set the PATH environment variable in the run configuration settings (Run -> Run Configurations... -> Environment).