Thread: My program exits on me right away!

  1. #1
    n00b programmer
    Join Date
    Oct 2005
    Posts
    15

    My program exits on me right away!

    Hi,
    I just started programming three days ago and I am having a little problem. When I run a program I have made that doesnt require any input (like Hello World!), the DOS window will open and close with a second. I have found the only way to prevent this is to use this bit of code...
    Code:
     
    #include <iostream>
    
    unsigned short t;
    
    int main ()
    {
        std::cout<<"Press any key followed by enter to exit\n";
        std::cin >> t;
        
        return 0;
    }
    Help?

  2. #2
    1479
    Join Date
    Aug 2003
    Posts
    253
    Code:
     
    #include <iostream>
    
    unsigned short t;
    
    int main ()
    {
        std::cout<<"Press any key followed by enter to exit\n";
        std::cin >> t;
       
        system("PAUSE");
     
        return 0;
    }
    Help?
    not the best way but it works
    Knowledge is power and I want it all

    -0RealityFusion0-

  3. #3
    n00b programmer
    Join Date
    Oct 2005
    Posts
    15
    What do you mean not the best? It works great! Plus it displays that cool "Press any key to continue..." line of text! Thank you!

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    I think it is system and compiler specific function....not to sure.

    A more common way would be to use cin.get() instead.
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    system command is not a portable code...go for this
    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main()
    {
      // Your code here
    
      // Clean up the stream and ask for input
      cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
      cin.get();
    }

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    lol using system("PAUSE") would br much shorter

  7. #7
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    But system("pause") would only work on a limited range of computers. Try using it on a mac and it won't work.

    The other version will work on any platform.

  8. #8
    n00b programmer
    Join Date
    Oct 2005
    Posts
    15
    Quote Originally Posted by sunnypalsingh
    system command is not a portable code...go for this
    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main()
    {
      // Your code here
    
      // Clean up the stream and ask for input
      cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
      cin.get();
    }
    Lol, I understand about 25% of that code. Just started programming four days ago... Thanks for the help!
    Home is where the computer is
    I like the smell of the treats!
    Mmmm, Root Beer...

  9. #9
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Quote Originally Posted by aznboi
    lol using system("PAUSE") would br much shorter
    never use the system function. It invokes another program do the "pausing" for you. It's not standard C++ so it's makes your code less portable.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    never use the system function.
    I wouldn't say that - but in this specific case it is a bad idea - for more details and a correct explanation you can refer to the programming tips section of the website.

  11. #11
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Use this then....if you couldn't understood previous solution but don't use system
    Code:
    #include <stdio.h>
    
    intmain(int argc, char **argv)
    {
      printf ("Press ENTER to continue.\n");
      getchar (); /* wait for input */
      return 0;
    }

  12. #12
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Man you guys are fussy fuss pots. If his application is only for Windows, and it only prints Hello World, then surely the system() function is OK? I mean, sure, he COULD port 'Hello World' to Linux, but I don't think theres a lot of reason to do so.

    I'm sure it's a better practice to stay platform independant and stick to the standard library as much as possible, but theres time to learn that later - I think for now system("pause") is fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Call a program & still capture cmds
    By Zuconta in forum C Programming
    Replies: 3
    Last Post: 07-23-2003, 03:54 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM