Visual C++ 2008 Console Application closes instantly?
2008-07-28 13:12:38 UTC
So I'm learning C++, and whenever I type in a code for a typical console application like Hello World in Visual C++, the window just instantly closes half a second after I click debug. Is there something new in C++ that I didn't know?
/* This is a simple C++ program. Call this file Sample.cpp.
*/
#include using namespace std;
// begin at main()
int main()
{
cout << "C++ is power programming.";
return 0;
}
Five answers:
2008-07-28 13:21:42 UTC
Just add the following line before the return 0;
{ char ch; cin.get(ch); }
Your entire program should look like this:
/* This is a simple C++ program. Call this file Sample.cpp.
*/
#include
using namespace std;
// begin at main()
int main()
{
cout << "C++ is power programming.";
{ char ch; cin.get(ch); }
return 0;
}
conehead
2008-07-28 13:26:01 UTC
Not sure I understand your question, but here's what I think you're saying. You're running your app thru debug, (or even if you double click) and the window for your program opens and closes.
This happens because you have no breakpoints set (for debug) and no pauses. It just runs your program and exits.
If you want to be able to look at your run (and I always do), open a command prompt window. Navigate to the Debug folder that has your executable. Enter the name and run the program that way. When the program exits, the window is still open and you can look at your output.
Also, if you have an input file or a lot of output that you want to look at, you can pipe either or both an input and output filename to the executable.
Confused!
2008-07-28 13:23:15 UTC
Console applications in a windows environment terminate after running.
Your console program runs in this kinda environment when run in visual c++ 2008.
Try executing the executable through command prompt.
Or just add a statement to accept a character at the end.
It could be:
//Start
char ch;
cout<<"\nPress any key to continue\n";
cin>>ch;
//End
balk
2008-07-28 13:23:17 UTC
It is normal for the console window to close immediately after the program ends.
You need to insert code which makes the program pause before the 'return 0' line.
Include this line at the top of your source file:
#include
And before the 'return 0;' line, put in this line:
while (!_kbhit()) {}
?
2016-04-03 15:22:39 UTC
You should run console applications from a console.
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.