Thread: why do some programs work on some compliers while others dont?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    18

    why do some programs work on some compliers while others dont?

    ok so im using Dev-C++ complier. I just wrote the simpliest program ever yet I cant get it to run. I complied it and all but dos just pops up really quick and disappears. Here is the code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
        cout << "Never fear C++ is here!";
        return 0;
    }
    why wont this simple program run in this complier? I need to understand why and how to fix it, thanks

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    It does run, but once it finishes, the window closes. Run it from a console window or put something in that takes user input in order to see the results.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    is the return 0 code closing it? How do I run it from the console window? I want to understand why this program closes so I wont make this mistake in the future. Thanks

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    put

    Code:
    System ("PAUSE");
    at the end of the file before return 0. This is on the faq.

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Using system() is, in general, not recommended as it opens up some security issues. For little practice and learning programs, it's not a big deal. However, it's better to avoid it now so you don't get into the habit of it later.

    Running your program from the command line is the better option. How to do this depends on your OS. Assuming Windows, go to the Start menu, click run. Type 'cmd' and press enter. Go to the directory the program is in and run in from there. The console window will stay open after the program is completed.
    There is a difference between tedious and difficult.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Or use an IDE that keeps it open for you.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Or better yet, open your program through the console.

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I won't start getting into persona preference, but I say live easy now, then worry about what the hardcore do later.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    so to run it using the cmd commnad I would type what? The program is located in my documents/programs folder/neverfear.cpp

    how would I type that in the dos screen to run it? Thanks alot guys for the help.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    cd "Documents\programs"
    myprog.exe

    With suitable replacements for your actual directory and program name.
    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.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by indigo0086
    put

    Code:
    System ("PAUSE");
    at the end of the file before return 0. This is on the faq.
    Since C and C++ are case-sensitive, System is different from system. I think you meant system, with a lowercase s.

    [edit] http://faq.cprogramming.com/cgi-bin/...&id=1043284385 [edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Simply put

    Code:
    cin.get()'
    before return 0
    Double Helix STL

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    cin.get()'
    ->
    Code:
    cin.get();
    of course.

    The FAQ has a more elaborate implementation:
    Code:
    #include <ios>      // Required for streamsize
    #include <iostream>
    #include <istream>
    #include <limits>   // Required for numeric_limits
    
    void myflush ( std::istream& in )
    {
      in.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
      in.clear();
    }
    
    void mypause() 
    { 
      std::cout<<"Press [Enter] to continue . . .";
      std::cin.get();
    } 
    
    int main()
    {
      int number;
    
      // Test with an empty stream
      std::cout<<"Hello, world!\n" ;
      mypause();
    
      // Leave extra input in the stream
      std::cout<<"Enter more than one character" ;
    
      myflush ( std::cin );
      mypause();
    
      std::cin.get();
    }
    cin.get() is fine when there's nothing in the input buffer; but if there is, you need to get rid of it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    23
    I'm surprised people brought up system pause but not cin.ignore().

    Try putting cin.ignore(); at the end of your program and that should make the window stay open. Dev-C++ was the first compiler I used to program with. Almost all programs that I wrote in Dev had to have a cin.ignore(); at the end so the programs would not close so fast. I stopped using Dev when I found out that you can download MS visual C++ express and use it for free. With MS visual C++ express you don't have to include pauses to keep the program console window open.


    Hope this helps

  15. #15
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    With Code::Blocks you don't have to either.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do video capture programs work
    By silk.odyssey in forum Windows Programming
    Replies: 4
    Last Post: 10-07-2008, 09:00 AM
  2. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  3. Replies: 1
    Last Post: 03-12-2008, 12:10 AM
  4. Will console programs still work in WinXP x64?
    By trenzterra in forum C++ Programming
    Replies: 8
    Last Post: 04-24-2005, 03:46 AM
  5. If you are employed as a programmer, please look
    By Flood Fighter in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-28-2004, 02:35 AM