0

Hi I am using visual studio express 2013.I have never used vs before and so just to test it out I ran a simple c++ program where the user enters 2 integers and their sum is then displayed. The problem is that the console window appears and takes the inputs, but then immediately closes once the output is displayed. Please note that this happens right after all the inputs are taken and when the output is shown. Is there anyway to fix this? I have looked all over and cant find a solution. I have tried including a bunch of things such as the getch() function at the end of my program, and pressing ctrl F5 to debugg my programs,but nothing seems to work. Please help!!!

nz881
  • 73
  • 2
  • 9
  • See also [How to keep the console window open in visual c++?](http://stackoverflow.com/questions/454681/how-to-keep-the-console-window-open-in-visual-c). If you created the project as "Win32 console application" CTRL-F5 will keep the console window open. – Chris Drew Dec 25 '14 at 21:45

3 Answers3

0

I have been using this, int getchar ( void );

Get character from stdin Returns the next character from the standard input (stdin).

OR

use this from process.h

system("PAUSE");

This approach is for beginners. It's frowned upon because it's a platform-specific hack that has nothing to do with actually learning programming, but instead to get around a feature of the IDE/OS - the console window launched from Visual Studio closes when the program has finished execution, and so the new user doesn't get to see the output of his new program.

One decent approach is Debug.WriteLine

// mcpp_debug_class.cpp
// compile with: /clr
#using <system.dll>
using namespace System::Diagnostics;
using namespace System;

int main() 
{
   Trace::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
   Trace::AutoFlush = true;
   Trace::Indent();
   Trace::WriteLine( "Entering Main" );
   Console::WriteLine( "Hello World." );
   Trace::WriteLine( "Exiting Main" );
   Trace::Unindent();

   Debug::WriteLine("test");
}
Karthik Nishanth
  • 2,000
  • 1
  • 22
  • 28
  • The main problem with system("PAUSE") is that it creates a new process. That is not much of a problem but it seems so unnecessary. – Sam Hobbs Dec 26 '14 at 00:39
0

In debug mode, Before your main return you could put a breakpoint and you will be able to see your console and the result you are waiting for.

Mohamed Ali
  • 173
  • 5
  • If I could vote I would vote for this one. There is no need to put something in the code that is only needed for debugging in the IDE. – Sam Hobbs Dec 26 '14 at 00:41
0

Put

system("PAUSE");

wherever you need the program execution window to pause. If you're using int main(), usually it'll be right before you return 0

user2649644
  • 124
  • 1
  • 2
  • 12