Results 1 to 10 of 10

Thread: C++ Hello World

  1. #1
    Join Date
    Jun 2006
    Location
    USA
    Beans
    165
    Distro
    Gutsy Gibbon Testing

    C++ Hello World

    I'm learning C++ and I'm writing a basic program:
    Code:
    #include <iostream>
    
    int main()
    {
    	using std::cout;
    	using std::end1;
    	
    	cout << "Hello World!\n";
    	cout << end1;
    	return 0;
    }
    When I try to compile it I get this error:
    Code:
    ./hello.cpp: In function ‘int main()’:
    ./hello.cpp:6: error: ‘std::end1’ has not been declared
    ./hello.cpp:9: error: ‘end1’ was not declared in this scope
    I followed the book I am reading correctly, did I type it wrong?

    -Ryan H

    Edit: It was an error in my typing, the only difference between '1's and lowercase 'L's in the font is there is no tail on the 1's but there is on the l's. Seens backwards but now everything is fixed,
    Last edited by Ryan H; January 14th, 2007 at 04:43 AM.

  2. #2
    Join Date
    Mar 2006
    Beans
    19

    Re: C++ Hello World

    i'm glad i bumped into ur thread. i just started learning C++ half an hr ago and had the same prob!

  3. #3
    Join Date
    Sep 2005
    Location
    Cedar Rapids, IA, USA
    Beans
    545
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: C++ Hello World

    It is endl (end little L) not end1. Correct that, and your program should work.
    #399784 | Ubuntu User #287
    *** If you're going to program, install the damn build-essential package ***
    There is no such thing as Ubuntu specific programming
    Save the electrons - if you quote, trim!

  4. #4
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: C++ Hello World

    Yes, it's endl (with an L), think of it as standing for "end line"

    Also, if you are going to be using cout and endl it's easier to just use the entire std namespace.

    This is a pretty common "hello world"

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

  5. #5
    Join Date
    Mar 2005
    Location
    Malaysia
    Beans
    300
    Distro
    Ubuntu 6.06 Dapper

    Re: C++ Hello World

    Quote Originally Posted by xtacocorex View Post
    It is endl (end little L) not end1. Correct that, and your program should work.
    I blame the font of many books "l" and "1" look almost the same in certain fonts

  6. #6
    Join Date
    Nov 2006
    Beans
    Hidden!

    Re: C++ Hello World

    always prefer the "\n" over the endl since that .. i forget why i think it flushes the buffer or something. decades ago when i did c++ i rmember reading that in a good book

    cout << "Hello\n";

    is better ( & cleaner) than

    cout << "Hello" << endl;

  7. #7
    Join Date
    Mar 2006
    Beans
    199

    Re: C++ Hello World

    For reference, you should realize that using std::endl is not exactly the same as placing a newline in the output. Using std::endl actually forces the buffer to be flushed, whereas \n does not.

    Link: http://www.cplusplus.com/reference/i...tors/endl.html

  8. #8
    Join Date
    Jul 2006
    Location
    Aarhus
    Beans
    113
    Distro
    Ubuntu 6.06 Dapper

    Re: C++ Hello World

    Quote Originally Posted by lloyd mcclendon View Post
    always prefer the "\n" over the endl since that .. i forget why i think it flushes the buffer or something.
    It's actually the other way round. endl flushes the buffer, the newline character doesn't. It depends what to use where. As I understand it endl produces a very slight additional overhead as it causes the text to get printed here and now, whereas the newline character just hangs in the buffer until it is full, and/or it is flushed automatically.

    The only problem there is with \n is that if your program is off to do other things that keep your system busy the buffer might not be flushed and your text might not get printed. Best bet is to always end the last output output statement with an 'endl'.


    ...as the previous post said... sorry hadn't seen that...
    Mike says: "I'll treat this problem like my mattress and sleep on it."
    - The Young Ones.

  9. #9
    Join Date
    Jan 2007
    Beans
    62

    Re: C++ Hello World

    Great information. Thanks!

    Exactly what I was looking for, I'm so happy with my decision to load up Ubuntu,

    Thanks again.

  10. #10
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: C++ Hello World

    Quote Originally Posted by lloyd mcclendon View Post
    always prefer the "\n" over the endl since that .. i forget why i think it flushes the buffer or something. decades ago when i did c++ i rmember reading that in a good book
    Prime example of http://en.wikipedia.org/wiki/Cargo_cult_programming

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •