Thread: Quick As A Flash - Running my Programs

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    21

    Quick As A Flash - Running my Programs

    When I try to run my programs using Dev C++, it compiles fine but the window with the output pops up and then dissapperas straight away before I get to see anything.

    My Code-

    Code:
    #include <iostream>
    
    int main()
    {
        std::cout <<"Hello World!\n";
        return 0;\
    }

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    Code:
     #include <iostream> 
     
    int main()
    {
    	 std::cout << "Hello World\n";
    	 system("PAUSE");
    	 return 0;
    }
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by publikum
    Code:
     #include <iostream> 
     
    int main()
    {
         std::cout << "Hello World\n";
         system("PAUSE");
         return 0;
    }
    Don't use this code. Do not, do not, DO NOT. system() is bloody evil. Do this:

    Code:
    std::cin.ignore(80, '\n');
    std::cin.get();
    
    return(0);
    Much more portable, and MUCH MORE SECURE. If I replaced pause.com on your system with a trojan, you'd have issues. With this, I can't do that.

  4. #4
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Yeah, don't use system(). And btw. publikum, doesn't system() require <windows.h> to be included?
    But anyway, make the program of yours wait for a respond of some kind. Like Lithorien wrote.
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    No, I'm 100% sure that system() works with iostream....

    I didn't know that's unsafe...
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    As far as I know, there may be no foolproof and portable way to pause your program. The cin.get() method seems to be one of the better options though (flaw: If you pipe input from a file to cin, get() will have no effect). One of the more 'reliable' methods is to include <windows.h> and do a MessageBox(). It's very hard to find a situation where this will fail to pause your program. Of course, it's non-standard and windows-specific, and more of a hassle to do. Another method is to just run your program from a command-prompt; that way it won't close no matter what. But, of course, that's even more of a hassle than using MessageBox()

    So in the end, we conclude that getting your program to pause when it finishes is usually of little concern; use whatever works for the particular program, and when you move past the 'beginner' phase, you'll find that mostly what's important is what happens while the program is running - what data it processes and how it does it, where it comes from and where it goes - not what gets displayed right before the program quits, and therefore it isn't super important to pause the program at the end. Of course, system() can still be dangerous, so as long as you stay away from obvious hazards, don't bother yourself too much about how pretty the program looks right before it dies
    Last edited by Hunter2; 01-29-2005 at 04:45 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I'm going to overkill this question lol:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout <<"Hello World!\n";
        std::cin.get( );
        return 0;
    }

  8. #8
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    explain to us why in the hell you would do that dude....

  9. #9
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Hunter2
    (flaw: If you pipe input from a file to cin, get() will have no effect)
    I didn't know that. But why in the world would anyone ever pipe input from a file into stdin on a program? You'd think you'd want a different way of handling that.

  10. #10
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    >>I didn't know that. But why in the world would anyone ever pipe input from a file into stdin on a program?

    Say each time you ran a program you had to enter a bunch of different options from stdin, but they were always the same. Wouldn't it be best to just pipe input from a file with all the options in it so you wouldn't have to do it each time?

  11. #11
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Kybo_Ren
    >>I didn't know that. But why in the world would anyone ever pipe input from a file into stdin on a program?

    Say each time you ran a program you had to enter a bunch of different options from stdin, but they were always the same. Wouldn't it be best to just pipe input from a file with all the options in it so you wouldn't have to do it each time?
    Good point. Thanks!

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    If your using windows run command prompt and then run your program. If your using xp go to start and click run then type cmd then hit enter. After that go to the directory where your .exe file is for your program. It will save the trouble of it closing. Another way to stop this is maybe throw in a do while loop that asks the user if they want to exit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-09-2008, 11:09 AM
  2. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 11:08 AM
  3. Compare running programs' MD5 and terminate
    By ferenczi in forum C Programming
    Replies: 3
    Last Post: 02-08-2008, 02:56 PM
  4. Running programs - few stupid mistakes
    By Korhedron in forum C++ Programming
    Replies: 14
    Last Post: 03-10-2004, 03:10 PM
  5. C++ code for running dos programs
    By danielp in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2003, 01:51 AM