unable to see the output of c++ program in the console - c++

I have installed the eclipse ide(cdt) on my windows 8 laptop and tried writing a simple c program to check whether the program executes.
It did not execute and gave the error : binary not found .
So I did some searching online and realized that my system did not have a c/c++ compiler installed.
So I installed MinGW and selected the c and c++ compilers during installation.
Then I set the PATH environment variable to C:\MinGW.
I reopened eclipse, wrote a simple c program and it worked as expected!
I created a c++ project, wrote a simple piece of code and could not see the output in the console!
Here is the code:
#include<iostream>
using namespace std;
int main()
{
cout<<"sample text";
return 0;
}

Linker (Option) > Add Command (g++ -static-libgcc -static-libstdc++)
This is not the right solution.
You have in your path environment variable only c:\minGW .
But it should be c:\minGW;c:\minGW\bin . (Set the PATH before open eclipse)
Therefore, libstdc++-6.dll needed by the current program, can not be found.
In eclipse there is no error, but no output in the console !!
It to compile into the program may be regarded as a trick, but will only work for the standard libs .
your linker flags should not be set like :
--> MinGW C++ Linker (Option) > Command (g++ -static-libgcc -static-libstdc++)
should be set here :
I know in this case it is not necessary at the end << endl to write.
A good programming style should use << endl :
cout << "sample text" << endl;

You may simply need to flush the output, using flush or endl. Try this:
cout<<"sample text" << endl;
or
cout<<"sample text" << flush;

Related

Clion `cout` and `cin` combination causes console to not work properly

I've recently started trying out CLion for C++ programming. I wanted to test a sample application (below):
#include <iostream>
int main()
{
std::cout << "Please enter a number: ";
int x;
std::cin >> x;
std::cout << "Your number was " << x << "!\n";
return 0;
}
This is what I was expecting (the number is user input):
Please enter a number: 10
Your number was 10!
And this is exactly what happens when I compile and run manually (g++ main.cpp -o main && ./main)
However, this is what happens when I run with CLion:
Does anyone know why this is happening, and how I can fix this?
Note: I am using CLion with the g++ compiler (version 9.3.0) on WSL2
After some more searching, I came across this StackOverflow post, which led me to this issue (upvote it!) which finally led me to do what was told in the comments:
Two workarounds are available:
Turn off PTY: by disabling run.processes.with.pty option in the Registry (Help -> Find Action -> Registry...)
Use Cygwin64 instead
I did the first option and CLion works fine now:
It looks like this is an issue with MinGW and WSL.

Necessary extensions for VS code with Windows OS

I am a first time C++ user, and I have been working for 8 hours trying to build and compile the simple "Hello World" program with C++ in Visual Studio Code. I have CygWin64, but I'm not sure if it's connected to my VSCode. I have installed the extensions C/C++, C/C++ Compile Run, C++ Intellisense, Clang-Format, and Easy C++ projects.
So far I have tried
#include <iostream.h>
main()
{
cout<< "Hi there";
return 0;
}
and
#include <iostream.h>
int main() {
std::cout << "Hello Easy C++ project!" << std::endl;
}
Using iostream.h helped me to get to work (it wouldn't at first), but I'm not sure if that is helpful, since other posts say that .h is very archaic. I have also tried editing my c_cpp_properties.json file. Sadly, I still get the message:
"> Executing task: bash -c "make run" <
'bash' is not recognized as an internal or external command,
operable program or batch file.
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it."
I am not sure if I need to install an alternative to Clang (I haven't found one), or run something initially on Cygwin64.
I have been looking online for suggestions and following pages such as https://dev.to/acharluk/developing-c-with-visual-studio-code-4pb9 and https://github.com/Microsoft/WSL/issues/1598, but I still can't seem to get around this problem.
Any help would be very appreciated.
Thanks,
Anne

C++ run error when using endl

I've recently started to learn C++ and I'm trying to compile and run a very simple program.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "Hello Again!";
return 0;
}
The program itself compiles as it should without any errors, however, when the program runs, it seems to stop after cout << "Hello World!" << endl;. I find this very strange, as my friend is sitting right beside me, doing the exact same thing and it works for him. The same thing happens when I try to use the sizeof();; it does not return any value, however, when my friend does this, it works.
When I ran it in NetBeans, it first generated the error
RUN FAILED (exit value 255, total time: 2s)
And another time I ran it, it generated the same error, but with a different exit value. Although it is now back to 255.
When running debugger in NetBeans it produces
SIGILL (Illegal instruction)
a few times before it stops working.
I have installed the MinGW compiler at the default directory (C:\MinGW), and this is the compiler that NetBeans and any other program is using. I have also added the path to the System Environment Variables at the end of the "Path" variable:
;C:\MinGW\bin;C:\MinGW\msys\1.0\bin
Trying to run and compile the same code in Atom results in
Hello World!Press any key to continue . . .
I have tried reinstalling the compiler, and restarted my computer. None of which seems to work. I've also tried \n, which works.
My question is, is there anything wrong with my compiler or computer, or am I missing something obvious? And is it possible to fix this?
(Sorry if this is a duplicate, I've searched for a few hours, not able to find anything useful)
I found the culprit!
In my System Environment Variables, C:\MingGW\bin and C:\MingGW\MSYS\1.0\bin was at the bottom of the list. This meant that it was below C:\Program Files (x86)\GNU\GnuPG\pub (Which I think, if I'm not mistaken is another compiler).
Although all paths in NetBeans were correct, it seems like the system didn't like it when another compiler was listed above MinGW.
I solved the problem by moving the paths for MinGW up, above the GNU.

Eclipse- C/C++ <terminated, exit value: -1073741515>

I recently decided to learn C/C++ in preparation for a coding class I will take in a few months so I downloaded and installed Eclipse. When I was going through the tutorials for the HelloWorld project, I ran into a problem where even though the code compiled perfectly fine, the console would not output "HelloWorld!" When I ran the debugger, it said that it was terminated and that the exit value was -1073741515 followed by my directory "C:\Users\Example\workspace\HelloWorld\Debug\HelloWorld.exe" followed by the date and time.
I installed MinGW and I set my path for eclipse to C:\MinGW\bin which is where it is in my directory and I checked to make sure my preferences were right because prior to this I was having some "program g++ not found in PATH" and "program gcc not found in PATH" These errors were fixed when I changed the environment variables.
When I continued to follow the tutorial, along with the HelloWorld.cpp which contained this code
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
Next, they told me to write a main.cpp which contained this code
#include <iostream>
using namespace std;
int main() {
// Say Helloworld five times
for (int index = 0; index < 5; ++index)
cout << "HellowWorld!" << endl;
char input = 'i';
cout << "To exit, press 'm' then the 'Enter' key." << endl;
cin >> input;
while(input != 'm') {
cout << "You just entered '" << input << "'. "
<< "You need to enter 'm' to exit." << endl;
cin >> input;
}
cout << "Thank you. Exiting." << endl;
return 0;
}
Finally, they told me to create a makefile to help build and run my project which contained this code:
all: hello.exe
clean:
rm main.o hello.exe
hello.exe: main.o
g++ -g -o hello main.o
main.o:
g++ -c -g main.cpp
What this program should be doing is just printing out "HelloWorld!" five times in the console, but it is not and is just returning the "terminated, exit value: -1073741515" I'm really confused as to why this is. Can someone please help me? Thank you all.
I have some experience with Eclipse C/C++
The first thing that I suggest is that you do a test by starting a new project and then click on the 'new hello world' option rather than 'empty project' that should set up all of your project. - When you have done this you should be able to run the program using the buttons in Eclipse.
If it works you are done - you can modify the program to whatever you wish and everything is made for ou.
If it does not work you have an issues, most likely with Eclipse not finding the compiler. You may be able to tell this when you start a new project as it will show the compilers that it has found on the right hand side of a start project window.
Hope this helps. In my experience Eclipse is really great, but has so many options it can be a little daunting and I know I don't use all the potential it has.
If you are really stuck try using PELLES instead of Eclipse - in my experience this is alot easier to get started with.
Your path settings may not be carrying through to your toolchain. To be sure, add the full path to your MinGW binaries folder to your Windows path and restart. (Cygwin binaries folder (C:\Cygwin64\bin, on my system) for Cygwin users).

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).