Thread: C++ cout

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    Talking C++ cout

    Hallo, I have been frustarting myself with trying to dispaly anything on my console application using cout but i just can seem to do it.
    Pleas help

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What compiler/IDE do you have? What have you tried? What errors are you getting?

    Try this:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello World" << std::endl;
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    Talking

    Thanks this seems to be the key, i am using the .NET frame work.

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I'm really new to C++ but as far as I read and attempted to compile you need the:
    Code:
    using namespace std;
    So you could use just cout without the std::

    Example:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "Hello World\n";
        return(0);
    }
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    Quote Originally Posted by Devil Panther
    I'm really new to C++ but as far as I read and attempted to compile you need the:
    Code:
    using namespace std;
    So you could use just cout without the std::

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

    While that's true, once you start working with multiple namespaces, you'll wish you'd used std::. Using std:: also helps you understand what is in the namespace std and what is not.
    Last edited by FoodDude; 09-14-2005 at 03:16 PM.

  6. #6
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I really wouldn't know about multiple namespaces, I just started C++ last week or so... finally moved on from C

    But can you please explain what do you mean by multiple namespaces, and why is it better to work with std:: ?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    I'm fairly new to c++ but many years in programming other languages.

    Let's say you write a program with a lot of cool custom functions. You could place all those functions in a namespace. then, you'd reference those functions with that namespace qualifier:

    using namespace cool;

    ...
    ...
    std::string blandtext;
    std::string fancytext;

    fancytext = cool::fancyit(blandtext);

    This doesn't seem like much...now have several custom namespaces for different types of formatting, printing, file stuff, ANYTHING. When you are working through your code with all types of custom functions, it will be easier to recognize your functions from std functions and even which type of custom fuction you are using at that time. Write a large program. Walk away for 6 months. You get an email to add some functionality. Now go in to make the changes. The easier it is to read, the easier to modify the code.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    once you start working with multiple namespaces, you'll wish you'd used std::
    If you really wanted to do that couldn't you just use std:: at the beginning with
    Code:
    using std::cout;
    or is that the same idea as namespace std?
    And whoever said death to Visual Basic, what is bad about it because I asked for a Visual Basic .NET book for my birthday (being foolishly new to standalone apps just having come from the world of JavaScript about a month ago).
    Last edited by beanroaster; 09-14-2005 at 03:34 PM.
    Adam

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I see... One last thing, what are namespaces??? Since books and guides use them, but non of them really explain the subject... is it like a class?

    p.s: I'm sorry if this is a really stupid question
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by beanroaster
    And whoever said death to Visual Basic, what is bad about it because I asked for a Visual Basic .NET book for my birthday (being foolishly new to standalone apps just having come from the world of JavaScript about a month ago).

    yeah... well it's easier than C/C++ and limited as well, I simply don't believe it's a good language to start with (like many people do) nor I belive it's a good lanauge to develope heavy or even mideum size apps. But that's just me
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  11. #11
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    http://www.cplusplus.com/doc/tutorial/tut5-2.html

    This keeps everything out of the global namespace, in it's own seperate scope in a project. All ANSI C++ header contents are loaded into the std namespace, which is why you see things like std::cout, std::pow, std::printf, std::whatever. The using namespace std; is somewhat of a battle, but I'm sure in very large unwieldy project, multiple coders, you would probably be happy you didn't stick it in a global header, but I'm not all that sure. It sure does make your code look l33t and verbose to reference absolutely everything through the global or std namespaces though.

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Quote Originally Posted by Devil Panther
    I see... One last thing, what are namespaces??? Since books and guides use them, but non of them really explain the subject... is it like a class?

    p.s: I'm sorry if this is a really stupid question
    a namespace isn't a class it is as it says a namespace. It is kind of hard to explain.
    Code:
    #include <iostream>
    
    namespace ns
    {
        void addSomething()
        {
            std::cout<<5 + 7<<std::endl;
        }
    }
    void addSomething()
    {
        std::cout<<2 + 4<<std::endl;
    }
    
    int main()
    {
        addSomething();
        ns::addSomething();
        
        std::cin.get();
        
        return 0;
    }
    This is a generic example but it gets the point across what a namespace does.
    Woop?

  13. #13
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by beanroaster
    If you really wanted to do that couldn't you just use std:: at the beginning with
    Code:
    using std::cout;
    or is that the same idea as namespace std?
    using std::cout is sort of the same idea as using namespace std;. using namespace std allows all of the objects/functions of that namespace to be used without the std:: in front, which could cause conflicts if any other part of the program had a thing named cout, etc. using std::cout poses the same problem: if theres another thing named cout in the program it could potentially cause a conflict, however since its only using std::cout, cin and the rest of the namespace std wouldnt pose any problem ever, where as with using namespace std its using the entire namespace and potentially could. Then theres std::, and that poses no problems ever unless you happened to name one of your namespaces std
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  14. #14
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    Then theres std::, and that poses no problems ever unless you happened to name one of your namespaces std
    Thats what I did the first few days of my C++ programming and all of those colons got really annoying once I started making bigger things.
    Adam

  15. #15
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by beanroaster
    Thats what I did the first few days of my C++ programming and all of those colons got really annoying once I started making bigger things.
    I find they make my programs look programming-like complex so I like using it for that reason.. sort of like I know what the hell I'm doing.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class methods to cout stream
    By shintaro in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2008, 07:27 PM
  2. cout vs std::cout
    By none in forum C++ Programming
    Replies: 10
    Last Post: 07-26-2004, 11:20 PM
  3. changing cout output
    By Yohumbus in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 04:09 AM
  4. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 04:17 PM
  5. printf vs cout
    By RyeDunn in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2002, 04:26 PM