C++/G++ hello world application issue, maybe compilation - c++

I am trying to compile my first c++ file on windows with the g++ compiler...
My cpp file is the following -
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
I type in this on command prompt to get to the directory
cd C:\Users\Mark
Then to compile my program I do
g++ hello.cpp
It creates a file name a.exe (default) but when I click on it the command prompt quickly flashes open then it's gone.
What did I do wrong? It should say Hello World! on the prompt and stay there, right?

What you did looks correct, but if you start the program by double clicking it in Windows, the new command prompt will be closed once it is finished. Since you already have a command prompt open for compilation, try to start the program from there, too:
g++ hello.cpp -o hello.exe
hello.exe

That's just normal Windows behavior of executing a command-line file from the UI -- as soon as the program exits (immediately in your case), the prompt window closes.
Running it from the command line will keep it up. To do that, Start > Run > cmd, then cd to the directory, then type a.exe.
Alternatively, you can wait for input from the user to keep the window open. That's fine for testing, but nobody who actually executes programs from the command line would want to have to hit a key to stop a program from running, when it should exit on its own correctly.

you didnt do anything wrong. There isnt a readline or anything at the end of your program, so when it is finishes executing it closes. Add a read character at the end of the program to make it wait for input to terminate.

You did everything right. Whether or not the command prompt stays open has nothing to do with your program. The command prompt is itself a program, and its default behavior is that when it launches in response to the execution of a console application, it closes immediately when the program is finished.
What you should do is launch the command prompt yourself and then run the program from within it, rather than launching the program by double-clicking its icon.
Do not fall into the bad practice of adding a call to some sort of pause function, or waiting for input at the end of the program. You should not get in the habit of hard-coding work-arounds for unwanted behavior of a particular shell into your application. Just use the shell the right way to get the behavior that you want.

The prompt has nothing to do with your program or the language. It has to do with your OS.
Your program should be run from the command line, which will (obviously) leave the command window up when it's finished.
There are tricks to make it stay up if you really want, but these are just tricks, and not necessarily good practice. One would be:
int main()
{
std::cin.get(); // waits for enter
}

Related

Can't execute compiled C++ exe file

I am having trouble executing my C++ code. I have written a basic "Hello World" program, and compiled it using the g++ make command. Here is my code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
I am on Windows 10, using Emacs for code editing, and CygWin for compilation. I saved this file as hello.cpp. I then navigated to the directory in CygWin. Then I did the command make hello. This created hello.exe. Then, I attempted to execute the file using ./hello.exe. I also tried ./hello which also didn't work. When I type one of these commands and hit Enter, it just on the next line, not doing anything. I can type in this blank line, but it won't do anything. Does anyone know a way to make my code execute properly. Thank you.
EDIT: I tried running this at cpp.sh, an online C++ compiler, and it worked fine.
Your program probably is working but the console window is closing before you can see anything.
Try adding an input at the end of the program so it will wait.
I.E.
int a;
cin >> a;
Your code is most likely executing, but not outputting anything. That's because it's failing. Try checking the return value after it has run with echo $?. If it's not 0 then it has crashed. Also run it in gdb and see if it fails. The reason why it's failing is most likely a windows/cygwin clash - it's not your code.

Why does the window disappear instantly after my program finishes running? How can I make it stay around?

Why do i have to put system("pause") after every program otherwise my programs run but the window just flashes and disappear. And after putting system function it runs and gives an option to press a key to continue. i am using dev c++ 4.9.9.2
The program is finished executing. There is nothing else left for it to do.
If you would like, you could replace the system call with std::cin.get();
I suppose you're using Windows so try this:
Either
make a shortcut for cmd.exe in the folder where your compiled binaries reside (make sure execute in [shortcut properties] is the current folder) or
hit Windows+R, type cmd, navigate to your executables directory using cd command
to obtain a windows command line window that will not close.
Now you can use
Myexe.exe
an the window will remain open. (You can even clear it to have a fresh empty window by typing cls.)
Now you can terminate your program normally.

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

Run C++ program from terminal. Get output in same terminal window

When I run my c++ programs from Terminal (Mac OS X), output from programs is shown in a new Terminal window.
What can I do to prevent the new window, and just have the programs' output straight in the window thats already open?
I believe gcc comes with the XCode tools package.
If you have gcc installed, open terminal window, cd to the directory where you put your cpp file, and type:
g++ myTestFile.cpp -o main; ./main
Replace 'myTestFile' by the name of your file, naturally. you can also rename the 'main' which is just the name of the compiled module, which you need to run by typing ./main to retrieve the output of your code.
I guess you are using the open command.
$ open foo
This will open a new terminal window.
Don't use the open command if you want the program to run in the current terminal window.†
$ foo
† You should obviously leave out the dollar sign.

Somehow display C++ application using double-click on Ubuntu?

I'm totally new to Ubuntu and C++. Anyway, I have PHP experience.
I just created very simple application...
#include <iostream>
int main() {
std::cout << "Hello, world!";
return false;
}
Then compiled it....
g++ hello-world.cpp -o hello-world
But I can't open it with double-click on it, like I did on Windows 7. Only way to get that text printed is to do command...
./hello-world
Is it possible to open compiled file using simple double-click and then get that text somehow printed?
The program you wrote is a console application. In most Linux GUIs, by default if you open a console program from the GUI, the console output will not be displayed. You can either configure the GUI to open a terminal, or you can manually open a terminal and run it yourself.
When doing development, I highly recommend manually running the program - with using the GUI's automatic terminal window opening mode, the terminal will close as soon as the program terminates; so if the program crashes, the message will be lost. Manually opening a terminal ensures it sticks around after termination, so you can read the program's last messages before terminating.