Thread: Noob - Hello World Not Working

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    9

    Noob - Hello World Not Working

    I am new to programming in C++. I have a bit of experience in Python and ActionScript3. I decided to pick up C++ so I downloaded Dev-C++ complier.

    I tried to run the following code but it doesn't seem to do anything. I complied the code, and it did not show any errors. I went to run it and a window appeared really quickly on the screen. It then closed itself after 0.5 seconds. I got the code from cPlusPlus.com.

    Do I have to configure a setting or something? I remember when I was working with Python, I had to reconfigure some sort of path directory for Windows.

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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, in Windows, when a program ends, its window closes. (When you shut down Word, you don't expect to still see your document up on the screen, do you?) You can read the FAQs on this very site for how to keep your program running.

    However, you haven't really made a Windows program -- you've made a command-line program that's meant to be run from a terminal window.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    What is a "command-line program" and a "terminal window"?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, go to Start -> All Programs -> Accessories -> Command Prompt.

    This is the mechanism by which you communicate with your computer.

  5. #5
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    In other words, something is needed to halt the program like something to get the user to physically close it...

  6. #6
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    >>Do I have to configure a setting or something? I remember when I was working with Python, I had to reconfigure some sort of path directory for Windows.

    No. I trust since it compiled without error, Dev-C++ has added mingw.exe (the compiler) to your path, so no worries there. Your program executes just fine. It just needs some massaging.

    CLUE: This is a non-issue for Code::Blocks or Visual C++ Express IDEs...

    Compile and run this:
    Code:
    #include <iostream>
    
    void sys_pause();
    
    int main(int argc, char *argv[])
    {
    	sys_pause();
    	return 0;
    }
    
    void sys_pause()
    {
    	std::cout << "Press [Enter] to continue...";
    	std::cin.ignore( 256, '\n' );
    }
    Last edited by StainedBlue; 09-13-2009 at 05:23 PM.

  7. #7
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Code:
    system("pause");
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  8. #8
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by ಠ_ಠ View Post
    Code:
    system("pause");
    ^never a good idea.

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    portable solution that works with every IDE

    Code:
    #include <iostream>
    #include <conio>
    using namespace std;
    
    int main (){
        cout << "Hello World!\n";
        cout << "Press any key to continue..."  
        getch();
        return 0;
        }

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    Thanks guys.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by abachler View Post
    portable solution that works with every IDE

    Code:
    #include <iostream>
    #include <conio>
    using namespace std;
    
    int main (){
        cout << "Hello World!\n";
        cout << "Press any key to continue..."  
        getch();
        return 0;
        }
    Why do you have the \n and the next cout line in red?
    Oh wait, you forgot the ; at the end of the cout line.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by cpjust View Post
    Why do you have the \n and the next cout line in red?
    Oh wait, you forgot the ; at the end of the cout line.
    Not to forget that \n shouldn't be used in this case, but rather std::endl.

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    Got it working. Thanks

  14. #14
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by cpjust View Post
    Why do you have the \n and the next cout line in red?
    Oh wait, you forgot the ; at the end of the cout line.
    yeah it was a bit of a quicky reply.

    The red shows the changes I made to the OP's code.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by abachler
    portable solution that works with every IDE
    How is that considered portable? It won't work on my system at all. Unless by "portable", you mean portable across Windows compilers, in which case it may be true.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too much to ask ?
    By deflamol in forum C Programming
    Replies: 2
    Last Post: 05-06-2004, 04:30 PM
  2. Hello World 180 KB???????
    By Musicdip in forum C++ Programming
    Replies: 17
    Last Post: 06-22-2002, 07:27 PM
  3. Who should rule the world?
    By CoderBob in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 02-07-2002, 07:01 AM
  4. Religious Bull****
    By Witch_King in forum A Brief History of Cprogramming.com
    Replies: 119
    Last Post: 10-23-2001, 07:14 AM
  5. No More Technology After World War Three
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-20-2001, 07:02 PM