-3

I have a new project where I created a HelloWorld.cpp Source file.

But when I am running it in Start without dedugging mode (CTRL+ F5), it opens the console and closes automatically.

#include <iostream>
#include<stdlib.h>
#ifdef _WIN32
#define WINPAUSE system("pause")
#endif
using namespace std;
void main()
{
    cout << "Hello, World!" ;

}
Joseph Willcoxson
  • 5,095
  • 1
  • 14
  • 23
Ayman Patel
  • 417
  • 2
  • 6
  • 15
  • 4
    Possible duplicate of [Preventing console window from closing on Visual Studio C/C++ Console application](https://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio) – Sneftel Jun 08 '18 at 10:40
  • I saw that post..none of them works ........but why is there downvote i tried those things and it does not work :( – Ayman Patel Jun 08 '18 at 11:15
  • It's a legitimate question. I removed the C++ tag. That was likely some of the down votes--it is not a C++ question, but a question about tools. – Joseph Willcoxson Jun 08 '18 at 13:49
  • and your VS version? – UKMonkey Jun 08 '18 at 13:50

1 Answers1

1

Mr. Patel, did you try using the second solution on that linked question and then tried to use the run without debugging option? The Visual Studio will only keep the command prompt open if you set the subsystem option in the linker to console. If it is not set, the window will close as soon as the program finishes running.

At any rate, note that this will only work if you run your program from inside Visual Studio, running your .exe directly will still have it close as soon as possible. If you want your program to wait on the user, you would need to do it yourself (at least as far as I know). A very simple solution would be to write your main function like this:

int main (int argc, char* argv[]) {
     ...//Your code goes here.
     std::cout << "Enter any character to end the program.\n";
     char end;
     std::cin >> end;
     return(0);
}

Note that to use the cin and cout streams, you should include the iostream header in your code.

Alex
  • 68
  • 5