Results 1 to 9 of 9

Thread: I might be retarded...

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    I might be retarded...

    I'm trying to learn C++ using Borland C++ v5, but I can't even get a hello world program to work. Can anyone help me out on why or give me a beginner sample project to look at?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Code:
    #include <iostream>
    
    int main()
    {    
        std::cout << "Hello world!" << std::endl
    
                  << std::endl
    
                  << "....." << std::endl
    
                  << std::endl
    
                  << "Goodbye, cruel world!" << std::endl
    
                  << std::endl
    
                  << "........." << std::endl
    
                  << "BANG" << std::endl;
    
        return 0;
    }
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Aragorn
    Guest
    you shouldn't need all these std::'s.

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    You do if you don't have
    Code:
    using namespace std;
    at the top of your code. By doing that you place yourself in the std namespace, which is the namespace all the functions in the standard library (which includes those in <iostream>) reside in. If you don't put the using statement in then you must explicitly qualify the scope of your calls to cout otherwise the compiler will be looking in the global namespace, which isn't where they are.

    Of course, if you use Microsoft Visual C++ and use this #include line:
    Code:
    #include <iostream.h>
    instead of using the C++ standard header with this line:
    Code:
    #include <iostream>
    then you will get the Microsoft header file, which is non-standard. Using the MS header file either:

    a) places you in the std namespace
    or
    b) places the standard functions, or an interface to them, in the global namespace

    I don't know which it does, but frankly I don't like either. I'd much rather keep my code standard and portable than make it dependent on Microsoft's IDE and have it clean up my mess.

    I take it you use the <iostream.h> header?
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    denniswrenn
    Guest
    You will unless you did this (inside of main, before any other code):

    Code:
    using std::cout; using std::endl;
    Or

    Code:
    using namespace std;

  6. #6
    To settle everything, this WILL work, provided you have your editor set up and everything (I use BTC++4.5):

    Code:
    #include <iostream.h>
    
    int main()
    {
        cout << "Hello, world!" << endl;
        return 0;
    }

  7. #7
    Megatron
    Guest
    It won't, because you did not specify to use the std namespace.
    Code:
    #include <iostream.h>
    using namespace std;
    int main()
    {
        cout << "Hello, world!" << endl;
        return 0;
    }
    Or if you write:
    Code:
    #include <iostream.h>
    int main()
    {
        std::cout << "Hello, world!" << std::endl;
        return 0;
    }

  8. #8
    Zaei
    Guest
    Apparently, #include <iostream.h> will put you into the standard namespace, while <iostream> will not. As a side note, iostream.h using TC++ 3.0 is the same as using it in VC++ 6.0. It seems logical that iostream.h is an ANSI standard C++ library, similar to the STL container classes.

    Z.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    *sigh*

    iostream is the standard, along with cstdlib, string, vector, etc. You shouldn't use the .h versions any more - they've been deprecated.

    Therefore, Harry is correct Listen to that man, for he speaks sooth
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

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



Click Here to Expand Forum to Full Width