Thread: Novice Beginner: Simple hello world wont work

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    53

    Novice Beginner: Simple hello world wont work

    Hey guys I did the following code and it still would not work

    Code:
    #include <iostream>
    
    int main()
    {
    std::cout<<"Hello World!";
    return 0;
    }
    I dont get why this won't work. What do I do?

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    49
    The code seems good. What compiler are you using?
    Hello, everyone.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What do I do?
    Well a better description than "doesn't work" would help for a start.
    Like
    - which operating system you're using
    - which compiler you're using
    - does it compile, if not, what error message(s) do you get
    - does it run, if so, what do you see (or not see) compared to what you expected to happen.
    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
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    I think it works its just that it closes before you can see it

    Code:
    #include <iostream>
    
    int main()
    {
    std::cout << "Hello World!";
    system("PAUSE"); // this pauses the program before it exits
    return 0;
    }
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    more ways there

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    I'm using Microsoft Windows XP Professional. I'm using Microsoft VIsual C++ 6.0 Professional also. I got it to work however by doing using namespace std; below the inclusion of the header file and using simple cout<< without the std:: in front of it. For some reason my compiler doesn't like the std:: very much. Thanks for the help guys. I'll have some questions on object oriented programming soon.

    Herns

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    49
    Update your VC6 with SP5, and it will like std:: very much also.
    Hello, everyone.

  7. #7
    FOX
    Join Date
    May 2005
    Posts
    188
    Try including a newline '\n' character in the string in case the output is line buffered.

  8. #8
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Quote Originally Posted by ^xor
    Try including a newline '\n' character in the string in case the output is line buffered.
    No. A \n doesn't flush the buffer; don't use it with cout. Instead, use this:
    Code:
    cout<<"Hello world!"<<endl;
    However, the buffer will be flushed automagically when the program exits anyway, so it wouldn't be a concern in this program as far as it not working. (The buffer also flushes automagically when it's full or the operating system decides to flush it.)
    Away.

  9. #9
    FOX
    Join Date
    May 2005
    Posts
    188
    > No. A \n doesn't flush the buffer; don't use it with cout. Instead, use this:
    stdout output is line buffered by default if the text is sent to a terminal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  2. My function seems too simple to work..
    By face_master in forum C++ Programming
    Replies: 10
    Last Post: 05-27-2002, 02:58 PM
  3. Why doesn't this work? (simple)
    By Clyde in forum C Programming
    Replies: 3
    Last Post: 04-02-2002, 04:48 PM
  4. Replies: 2
    Last Post: 01-26-2002, 12:13 PM
  5. Major Beginner looking for simple pointers
    By dlkchic in forum C++ Programming
    Replies: 4
    Last Post: 09-05-2001, 12:27 PM