Visual C++ Program Termination

This is a discussion on Visual C++ Program Termination within the C++ Programming forums, part of the General Programming Boards category; Ok I know this probably yields a simple answer but when I used DJGPP and wrote a program such as ...

  1. #1
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96

    Visual C++ Program Termination

    Ok I know this probably yields a simple answer but when I used DJGPP and wrote a program such as this:

    Code:
    // This program will calculate the product of 3 integers.
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
    int x,y,z,result;
    
    cout<<"Enter three integers: ";
    cin>>x>>y>>z;
    result = x*y*z;
    cout<<"The product is "<<result<<endl;
    return 0;
    }
    It would execute and stay there so one could view the result. But now on Visual C++ the second I enter the 3 numbers the dos prompt disappears making it impossible to see the results. How do you prevent this?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Why does it work without that get thing in DJGPP but not in Visual C++? ARe there any other 'official' ways? Am I missing anything?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    In DJGPP, you run the program manually from the current command prompt, and when the program exits, that's where you return to.

    In VC++, if you manually start a command window and run the program manually, the situation is the same.

    HOWEVER, if you run it from the IDE, a new (and temporary) command window is created just to run your program, and it disappears as soon as your program exits. In this case, you need some kind of pause to stop the whole thing disappearing before your eyes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Do all programs need this?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Right. If you don't care to add a pause mechanism, just be sure to always run your program from the DOS prompt.

    ie: go to the start menu:

    Start->Programs->MS-DOS Prompt

    At the prompt, just go to the directory where your exe is located and invoke it, ie: the program 'myprog.exe' is in 'C:\Windows\VCC\Programs\', ie:

    C:\> chdir c:\windows\vcc\programs

    C:\WINDOWS\VCC\PROGRAMS> myprog

    If your program uses command-line parameters, you'll have to use the DOS prompt anyway...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    So I can never just double-click it? Double clicking works if I compile it with DJGPP but not MSVC++. Why is that?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Be more specific about what "doesn't happen"

    The answer is it should work, and seems to here
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Neo
    Neo is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    8
    you can pause it using a simple getch(); it just sits there and waits for a character from the user. the getch() function is in the <conio.h> library. this is all assuming you are using VC++

  10. #10
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Originally posted by Salem
    Be more specific about what "doesn't happen"

    The answer is it should work, and seems to here
    Ok This is EXACTLY what happens...

    I create a program that outputs something (like the previously stated example). I compile it in Rhide DJGPP. I leave the compiler and double click my .exe file. It runs the program and exits the program fine. But it keeps the DOS prompt up until I click the X at the top right.

    When I run my .exe compiled in Microsoft Visual C++ it starts the program. And as soon as the program finishes it closes the DOS window. Both of these situations the programs were run OUTSIDE of the compilers. I'm just asking why does it work in one and require an extra function in the other?

  11. #11
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    The "DOS" prompt does not exit on its own in the 9x Win32 based programs. The Win32 based OSes like NT, 2000, and XP generally do. The MSVC++ IDE automatically kills the prompt on its own.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  12. #12
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    OK Thanks.

  13. #13
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    This is an operating system issue. There is nothing "wrong" with your program, or the compilers.

    DJGPP makes true-DOS programs.

    Microsoft Visual C++ cannot make true-DOS programs. It can make Windows applications, and Windows Console applications which look like DOS, but won't run on a machine that's booted to DOS.

    I haven't tried this on WinXP, but on a Win98 machine, you can right-click on a true-DOS program, select properties, and check a "close on exit" box. You don't get this choice if you right-click on a Windows Console application.

  14. #14
    Neo
    Neo is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    8
    i didn't know that... thanks!

  15. #15
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Originally posted by DougDbug
    This is an operating system issue. There is nothing "wrong" with your program, or the compilers.

    DJGPP makes true-DOS programs.

    Microsoft Visual C++ cannot make true-DOS programs. It can make Windows applications, and Windows Console applications which look like DOS, but won't run on a machine that's booted to DOS.

    I haven't tried this on WinXP, but on a Win98 machine, you can right-click on a true-DOS program, select properties, and check a "close on exit" box. You don't get this choice if you right-click on a Windows Console application.
    You're right. Another good reason to use DJGPP

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Need help - error on program termination
    By RaySun in forum C Programming
    Replies: 6
    Last Post: 09-20-2004, 01:23 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21