We help IT Professionals succeed at work.

why ms-dos window disappears to fast

plonergan
plonergan asked
on
When I compile my program (with no errors)and try to then run it, the ms-dos window will flash quickly. How do I get it to stay open with output printed to the screen. When it flashes up, I can see that the ouput is there, I just don't know how to make it stay open. The problem is that the dos window closes too fast. I'm sure this is either a simple config problem or maybe a line of code in my program. Please help.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Invoke your program from a batch file, and add a "pause" command afterwards.  This will result in your program's output remaining in the window and the output window waiting for key input before it closes.
I think you use VC++.

Now, you can see output by pressing Ctrl-F5 or using menu  Build /  Execute

Author

Commented:
Stimpyjcat, I am using a scaled down version of Visual C++. I don't know how to invoke my program from a batch file and therefore don't know what the pause command is. Is there something I can add to my code: (Or if you want to explain the batch and pause)

#include <iostream>
#include <map>
using namespace std;

typedef map<int, double, less<int> > maptype;
typedef maptype::iterator Iterator;

void MyInsert(maptype &M, int k, double x)
{
   pair<Iterator, bool> P = M.insert(make_pair(k, x));
   Iterator i = P.first;

   bool b = P.second;
   cout << "After attempt to insert (" << k << ", " << x << "), returning P = (i, b):\n";
   cout << "(*i).first = " << (*i).first << endl;
   cout << "(*i).second = " << (*i).second << endl;
   cout << "b = " << b << endl << endl;
}

int main()
{
   maptype M;
   MyInsert(M, 800, 0.3);
   MyInsert(M, 800, 0.7);
   return 0;
}


Author

Commented:
Stimpyjcat, I am using a scaled down version of Visual C++. I don't know how to invoke my program from a batch file and therefore don't know what the pause command is. Is there something I can add to my code: (Or if you want to explain the batch and pause)

#include <iostream>
#include <map>
using namespace std;

typedef map<int, double, less<int> > maptype;
typedef maptype::iterator Iterator;

void MyInsert(maptype &M, int k, double x)
{
   pair<Iterator, bool> P = M.insert(make_pair(k, x));
   Iterator i = P.first;

   bool b = P.second;
   cout << "After attempt to insert (" << k << ", " << x << "), returning P = (i, b):\n";
   cout << "(*i).first = " << (*i).first << endl;
   cout << "(*i).second = " << (*i).second << endl;
   cout << "b = " << b << endl << endl;
}

int main()
{
   maptype M;
   MyInsert(M, 800, 0.3);
   MyInsert(M, 800, 0.7);
   return 0;
}


Commented:
Hi plonergan,

why not put

   getch();
   return 0;

as the last 2 lines before the app terminates. You could display "Press any key..." or something to wait for user input before closing the window.
Little side-note. The window that pops up is NOT an MS-DOS window. The system running is windows -- the window is just the input/output-console for a full win32-program without graphical user interface.

regards,
.:fl0yd:.

Author

Commented:
Floyd, thanks for the side-note, that was helpful. Can you please add the code you are speaking of to my code. I am new to C++ (taking a training class at work). I was educated in Pascal believe it or not.

Author

Commented:
Floyd, I added a couple of lines of code using cin and cout prompting the user to enter their name at the end of the program to try and stop it from terminating so fast. It still doesn't work.
#include <iostream>
#include <map>
//You should include this fo getch()
#include <conio.h>
using namespace std;

typedef map<int, double, less<int> > maptype;
typedef maptype::iterator Iterator;

void MyInsert(maptype &M, int k, double x)
{
  pair<Iterator, bool> P = M.insert(make_pair(k, x));
  Iterator i = P.first;

  bool b = P.second;
  cout << "After attempt to insert (" << k << ", " << x << "), returning P = (i, b):\n";
  cout << "(*i).first = " << (*i).first << endl;
  cout << "(*i).second = " << (*i).second << endl;
  cout << "b = " << b << endl << endl;
}

int main()
{
  maptype M;
  MyInsert(M, 800, 0.3);
  MyInsert(M, 800, 0.7);
  getch();//The code to wait, my friend!!!!
  return 0;
}

Author

Commented:
I have already tried this when floyd posted it.  I am getting an error message: "implicit declaration of function 'int getchar(...)'. Now it won't even compile.

Commented:
Hey plonergan,

when u use getch() function you need to include the "stdlib.h" file.

so if u use getch() in ur code use this too...

#include <stdlib.h>

try with this I think this might work for u.

Commented:
plonergan,

just to sum up what information we've gathered so far:

+ while debugging start your application with [CTRL]+[F5]. This ensures that the console window will stay open until a key is pressed.
+ in your release version you can't use this feature though. Your app will have to make sure, that the window doesn't close, before the user wants to. To achieve this do the following:

#include <conio.h>

// leave your code unchanged, except for the main-function:

int main()
{
  maptype M;
  MyInsert(M, 800, 0.3);
  MyInsert(M, 800, 0.7);
  getch();  // wait for keypress (try _getch() if you get an error.
  return 0;
}

That's it. My regards,
.:fl0yd:.

Commented:
Hi plonergan

include<conio.h>

and add

getch(); before your application terminates and you would be able to see the output and the screen would stay until you press a key.

Commented:
I once read a Microsoft KB article that said you could check whether the console was empty when your program started.  This would mean that your program had either started from Windows (so would close too soon), or from a batch file after a CLS command.

Once you've checked this, you can set a bool variable, saying whether you do a getch() at the end of your program.  I'm afraid I can't give you any code, and I can't find the MS article, but it's a start...
CERTIFIED EXPERT

Commented:
The alternative to modifying the code is to wrap the invocation of said code in a batch file, e.g. open "myprog.bat" in notepad.exe, and insert the following:

REM This is a batch file comment
c:\path\to\prog.exe /args /go /here
pause
Commented:
Here is the KB article:

****************************

Preventing the Console from Disappearing

PSS ID Number: Q99115
-------------------------------------------------------------------------
The information in this article applies to:

 - Microsoft Win32 Application Programming Interface (API) included with:

    - Microsoft Windows NT versions 3.1 and 3.5
-------------------------------------------------------------------------

SUMMARY
=======

When a console application is started from the File Manager, from the
Program Manager, or by typing "start <progname>" from the command prompt,

it executes in its own console. This console disappears as soon as the
application terminates, and therefore the user can't read anything written
to the screen between the last pause and program exit. To resolve this
problem, the programmer should pause the application before termination to
allow the user to read all of the information on the screen.

It is not likely that the programmer will want to introduce this pause if
the application is started directly from the command prompt, because in

this situation it won't make much sense to the user. However, there is no
API (application programming interface) that directly determines whether or
not the application shares a console with CMD.EXE. There is a method that
can be used to determine this information in most cases. When the
application first starts up, call GetConsoleScreenBufferInfo(). If the
cursor position is (0, 0), then the application has its own console, which
will disappear when the application terminates. Otherwise, the application

is operating within a console belonging to another program, typically
CMD.EXE.

NOTE: This method will not work if the user combines a clear screen (CLS)
and execution of the application into one step ([C:\] CLS & <progname>),
because the cursor position will be (0, 0), but the application is using
the console, which belongs to CMD.EXE.

MORE INFORMATION
================

To start a console application with its own console that will not disappear
when the application is terminated, use CMD /K. For example, use


   start CMD /K <progname>

Note that it is possible to programmatically force an application to always
have its own console by immediately doing a FreeConsole() and an
AllocConsole(). The disadvantage is that the C run-time handles are no
longer valid. Use CreateFile( "CONIN$", ... ) with lpsa->bInherit=TRUE, in
combination with _open_osfhandle() and dup2() to close the current handles
(stdin, stdout, stderr) and associate handles that will be inherited.

Additional reference words: 3.10 3.50

KBCategory: kbprg
KBSubcategory: BseCon

****************************

Explore More ContentExplore courses, solutions, and other research materials related to this topic.