Whats the difference between cout and std::cout?

This is a discussion on Whats the difference between cout and std::cout? within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm making a program, and I want it to be as neat as possible. And I was wondering if ...

  1. #1
    Registered User mdshort's Avatar
    Join Date
    Dec 2003
    Posts
    6

    Post Whats the difference between cout and std::cout?

    Hey, I'm making a program, and I want it to be as neat as possible. And I was wondering if there really was a difference between using std:cout<<""; rather than just using cout<<"";. I'm making an RPG, which I'm making for learning purposes.

    Also, I'm new to this, and I've read the tutorials, and I've come to find the explanations of pointers is very confusing. And its used in ways later on in the cprogramming.com tutorials in ways that it had'nt explained before. I'm aiming for a book soon, but for now im going to use this. Please explain pointers.

    And to make life easier for me and possibly for others, i was wondering how I could make a function to make colors easier to call.

    This is what I have so far:

    Code:
    int txtclr(int clr0, int clr1, int clr2, int drk)
    {
        HANDLE hConsoleOut;
        hConsoleOut=GetStdHandle(STD_OUTPUT_HANDLE);
        if ()
        {
        }
    }
    Thats only an example of how i'm thinking the program up. This is very mind boggling. How would I design a short function to make such a complex funtion complete? I could make an if statement for EVERY SINGLE variation, but that would be more work than needs to be put into it. I've thought of making it so that if its true, define a variable to be the FOREGROUND_COLOR part of the color definition, but how would I do that? I've tried just putting the variable into the color call:

    Code:
        SetConsoleTextAttribute(hOut,
        clr0[] |
        clr1[] |
        clr2[];
    When I do this I get an error, how would I implement this? I'm going to just use and if statement to see which colors are being used, but that would mean doing EVERY SINGLE variation, so it would be more work than just doing EVERY SINGLE variation of the color call. The program i'm making will use colors often, so I want to make this easier on me. Maybe if someone has a script which does this, or can help me develop this function we can have it as a resource for those who don't want to define the color with all the code it requires.

    If someone has a solution, thanks alot.
    Thanks in Advanced,
    -Mike

    [EDIT]
    Whew that was alot of typing.
    [/EDIT]
    Last edited by mdshort; 12-28-2003 at 11:11 PM.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> Hey, I'm making a program, and I want it to be as neat as possible. And I was wondering if there really was a difference between using std::cout<<""; rather than just using cout<<"";. I'm making an RPG, which I'm making for learning purposes.

    Firstly, you should be including <iostream> and not <iostream.h>. The first is in the standard library, the second no longer is. In the new C++ library, everything is placed in the std namespace. This means that you somehow need to resolve each identifier as being in the std namespace. There are three ways to do this:

    1. Not a preferred way. It dumps the entire namespace into the global namespace:
    Code:
    #include <iostream>
    using namespace std;
    
    int main( )
    {
      cout << "Blah..." << endl; // Both 'cout' and 'endl' (and everything else) has been dumped into the global namespace.
    }
    2. An alright method. This dumps only a particular indentifier into the global namespace, but not the rest:
    Code:
    #include <iostream>
    using std::cout;
    
    int main( )
    {
      cout << "Blah..." << std::endl; // 'cout' is in the global namespace, 'endl' has not been placed there.
    }
    3. The often preferred way. Don;t put anything in the global namespace:
    Code:
    #include <iostream>
    
    int main( )
    {
      std::cout << "Blah..." << std::endl; // Neither 'cout' nor 'endl' (nor anything else) has been dumped into the global namespace.
    }
    >> Also, I'm new to this, and I've read the tutorials, and I've come to find the explanations of pointers is very confusing. And its used in ways later on in the cprogramming.com tutorials in ways that it had'nt explained before. I'm aiming for a book soon, but for now im going to use this. Please explain pointers.

    Thats a fairly tall order. A book, and some tutorials will help greatly. Basically, a pointer is a variable that stores a memory address. That is, it 'points' to a specific spot in memory. You can access what is stored in that memory location by dereferencing the pointer:
    Code:
    int y = 0;
    int* p = &y; // 'p' stores the address of y
    *p = 5; // Access the memory block pointed to by p, and modify it.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User mdshort's Avatar
    Join Date
    Dec 2003
    Posts
    6

    What about the last bit I talked about?

    About the colors, and how to use variables to hold commands or conditions?
    [EDIT]
    Sorry, I forgot to thank you for the bit of code you gave me, thanks.
    [/EDIT]

  4. #4
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    technically, 'cout' is an identifier you can use for a variable name, and 'std::cout' is an identifier used for I/O. So when you say 'std::cout' you're saying "I want to use the standard cout and not some other one".

    writing std:: before it helps it be more visible that you're using the standard cout, you could say "using std::cout" at the top of the program or "using namespace std". The latter is considered naughty.

    pointers can be a tough thing to learn.

    just think of a pointer as a number, except the number corresponds to a place in memory, and the place in memory is where the actual value is stored.

    Code:
    int  Num = 100;
    int* p = &Num;
    This makes p point to the place in memory where Num is.
    Last edited by HybridM; 12-29-2003 at 12:35 AM.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Of course, you can also use a using directive or using declaration in local scope. It doesn't have to be global at all.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    SetConsoleTextAttribute(hOut,
        clr0[] |
        clr1[] |
        clr2[];
    Take off the square brackets, and add a closing parentheses, and it should compile.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    For the color thing, I have an easy way of doing it. I just made a header file called Color.h and included it (#include "Color.h")
    It goes like this:

    Code:
    #define Blue SetConsoleTextAttribute(hOut, FOREGROUND_BLUE);
    #define Red SetConsoleTextAttribute(hOut, FOREGROUND_RED);
    ... //and so on through the colors
    and then:

    Code:
    #include <iostream>
    #include "Color.h"
    //Code
    HANDLE hOut; //hOut or whatever, that's how I learned it :D
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    Red; //Or any color defined in Color.h
    cout << "Hello World!";
    //Code
    Which prints Hello World in red. This is really useful when making console graphics (Yes, this is possible, but it must be all coded in by setting the color and then using for the spaces).

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by SirCrono6
    For the color thing, I have an easy way of doing it. I just made a header file called Color.h and included it (#include "Color.h")
    It goes like this:

    Code:
    #define Blue SetConsoleTextAttribute(hOut, FOREGROUND_BLUE);
    #define Red SetConsoleTextAttribute(hOut, FOREGROUND_RED);
    ... //and so on through the colors
    and then:

    Code:
    #include <iostream>
    #include "Color.h"
    //Code
    HANDLE hOut; //hOut or whatever, that's how I learned it :D
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    Red; //Or any color defined in Color.h
    cout << "Hello World!";
    //Code
    Which prints Hello World in red. This is really useful when making console graphics (Yes, this is possible, but it must be all coded in by setting the color and then using for the spaces).

    - SirCrono6
    IMO this is poor use of a macro and you'd be better off simpy having a function and passing the colour as a parameter.

  9. #9
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    writing std:: before it helps it be more visible that you're using the standard cout, you could say "using std::cout" at the top of the program or "using namespace std". The latter is considered naughty.
    Not that I'm advanced or anything, but why is it naughty?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Because there are many things in the std namespace, and importing the whole namespace into your global namespace prevents you from having anything of the same name.

    Example: You have a file like this:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    ...
    
    char * copy(const char *source, const char *sourceend, char * dest)
    {
      ...
    }
    Unlikely that you have it, but you should get the idea. Now you decide you need the <algorithm> header.
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    This new code might not compile, or bring unexpected behavior. There is a template function called copy in the new header with a very similar signature to the existing one.

    The purpose of namespaces is to avoid name conflicts like this one. The using statement counteracts this effect by importing a whole namespace into the current (probably global) namespace, which is what had been avoided by using namespaces.

    In other words: if you're going to import the whole thing, why have namespaces in the first place?
    Last edited by CornedBee; 12-30-2003 at 04:47 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    A using directive (using namespace std) isn't "naughty" as such, but it beats the point of using a namespace in the first place if you simply make all of the entities in namespace std available, as the whole idea is to avoid name clashes. Of the alternatives, a using declaration (using std::cout) is slightly better, a prefix (std::cout) is by far superior.

    You don't have to place a using directive/declaration in the global namespace, it can have local scope. However, what *is* considered "naughty" is to place a using directive in a header file, forcing everyone who includes the header to make all of the entities in the namespace available in files that include it. That's very poor style.

    Note that a using directive and using declaration aren't totally equivalent at local scope, so code that compiles correctly with one may not compile correctly with the other - or worse, may have a hidden side effect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binding cout?
    By Elysia in forum C++ Programming
    Replies: 14
    Last Post: 10-16-2008, 07:41 AM
  2. cout and std::cout
    By alyeska in forum C++ Programming
    Replies: 15
    Last Post: 09-23-2007, 01:43 AM
  3. New, making a survey program
    By shaffer in forum C++ Programming
    Replies: 18
    Last Post: 12-01-2006, 11:36 AM
  4. Binary I/O with cin and cout
    By The Urchin in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 01:47 PM
  5. cout vs std::cout
    By none in forum C++ Programming
    Replies: 10
    Last Post: 07-27-2004, 12:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21