Thread: How to make the program remain open

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    38

    How to make the program remain open

    I have visual studio 2010 and when I run a program the command prompt window with the program opens and closes automatically. How can I make the window remain open without running the program from the command prompt.

    Code:
     //#include "stdafx.h"  was included on the other template
    #include <iostream>
    using namespace std;
    
    
    //int _tmain(int argc, _TCHAR* argv[]) was included on the other template
    int main()
    {
    	cout <<"she sells sea shells by the seashore"<< endl;
    	cout <<"\nshe"<< endl;
    	cout <<"sells"<< endl;
    	cout <<"sea"<< endl;
    	cout <<"shells"<< endl;
    	cout <<"by"<< endl;
    	cout <<"the"<< endl;
    	cout <<"seashore"<< endl;
    	cout <<"\nshe sells"<< endl;
    	cout <<"sea shells"<< endl;
    	cout <<"by the"<< endl;
    	cout <<"seashore"<< endl;
    	return 0;
    }
    
    /*1. Write on the screen the words
    she sells sea shells by the seashore
    (a) all on one line, (b) on seven lines, and (c) inside a box.*/

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Why did you comment the stdafx.h?
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    stdafx.h is a "magic" header file used by Visual Studio to implement "pre-compiled headers".

    It's a build-time optimisation which works well on large stable projects.
    For student homework sized problems (a handful of source files at the most), it's mostly an annoyance.
    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.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Two good solutions:
    Run program with without debug (typically Ctrl+F5). Prompt will not close.
    Or, put a breakpoint at the closing brace of main (typically F9) and run with debug (typically F5).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    38
    The first solution worked

    Thanks

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The second solution is handy when you need to debug. Don't forget it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by Elysia View Post
    Two good solutions:
    Run program with without debug (typically Ctrl+F5). Prompt will not close.
    Or, put a breakpoint at the closing brace of main (typically F9) and run with debug (typically F5).
    Kindly explain, why these solutions will work?
    I want to know the reason as i don't know.......
    I'll appreciate this..
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First one because Visual Studio for some reason adds a "press any key to close..." after the program has ended when not running in debugger.
    And the second because the debugger stops at the breakpoint allowing you to gaze upon the console before it closes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by Elysia View Post
    First one because Visual Studio for some reason adds a "press any key to close..." after the program has ended when not running in debugger.
    And the second because the debugger stops at the breakpoint allowing you to gaze upon the console before it closes.
    Thanks alot....
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To Make A Program Remain Open For Windows XP
    By BrandNew in forum C Programming
    Replies: 3
    Last Post: 02-22-2011, 08:28 AM
  2. how do you make a program send data to the printer?
    By Sintu in forum C++ Programming
    Replies: 19
    Last Post: 10-20-2005, 07:22 PM
  3. drawing over the open program.
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-23-2002, 03:37 PM
  4. Replies: 1
    Last Post: 11-23-2001, 10:01 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM