Exe file not opening

Viewed 825

I am trying to learn C++ now and created a Hello World program. When I compile it on Linux using g++ and it works perfectly fine. When I compile it on Windows using the Build tools, it still compiles the code into machine code, but I can't open the executable. I used the Microsoft build tools as a compiler. The code was:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!";
}

The output should be: Hello, World!

**Question already answered: The program closes because it is not run in cmd. To prevent the program from crashing add

```system("pause");``` 

at the end**

3 Answers

The executable is showing the correct output on the terminal, but that terminal closes that fast that you don't even realise it.

I'd advise you to open a command prompt, go to the directory where the executable is located and launch it over there. You'll see the desired output.

it possibility because the app closes immediately after ouputing add system("pause");

on the end

Its not that you can't open that exe file. Once you click on that file, it opens up and does its work and closes. To prevent your exe file from getting closed after doing its operation, you can add following line at the end of the main function :

getchar();

And now once you open your exe file, it wont close automatically, you will need to press "enter" to close it.

#include<iostream>
using namespace std;

int main(){
    cout<<"hello world";
    getchar();
}
Related