cout does not work

Discussion in 'C++' started by Brian, Sep 26, 2012.

  1. Brian

    Brian Guest

    I'm new to C++ and got a book to learn this programming language.
    I downloaded Microsofts Visual Studio C++ express.

    I found that code in the book does not seem to work
    If I enter cout << "Hello World";
    then Visual Studio C++ express reports this as an error.

    Does Microsofts version of C++ have its own non standard language?
    Is there a compiler that I can use the accepts the standard C++ language
    that the book has been written for?
    I'm using Windows7 64 bit operating system.

    --
    Regards Brian
    Brian, Sep 26, 2012
    #1
    1. Advertising

  2. Brian

    Brian Guest

    Paavo Helde <> wrote:
    > Brian <> wrote in news:159237127370317100.668440bclark-
    > :
    >
    >> I'm new to C++ and got a book to learn this programming language.
    >> I downloaded Microsofts Visual Studio C++ express.
    >>
    >> I found that code in the book does not seem to work
    >> If I enter cout << "Hello World";
    >> then Visual Studio C++ express reports this as an error.
    >>
    >> Does Microsofts version of C++ have its own non standard language?
    >> Is there a compiler that I can use the accepts the standard C++ language
    >> that the book has been written for?
    >> I'm using Windows7 64 bit operating system.
    >>

    >
    > See http://www.parashift.com/c -faq/posting-code.html
    >
    > Visual Studio is generally OK, maybe the problem is with your book.


    Thanks for your reply Paavo.

    From what I found on the internet it looks like you need to type

    Std::cout << "Hello World"

    Instead of
    cout ,<< "Hello World"

    So it looks like you need to tell Visual Studio C++ that you are entering a
    standard C++ command.

    I'm not certain what the library "stdafx.h" does or if it's needed just to
    display the words "Hello World".

    When I run this simple code the written for a console, the DOS window
    quickly pops up. is there a way for the DOS window to stay on the screen so
    I can read the words "Hello World" ? I was thinking of a user input
    prompt so the DOS window would wait for a reply from the user.

    --
    Regards Brian
    Brian, Sep 26, 2012
    #2
    1. Advertising

  3. Brian <> wrote:
    > When I run this simple code the written for a console, the DOS window
    > quickly pops up. is there a way for the DOS window to stay on the screen so
    > I can read the words "Hello World" ? I was thinking of a user input
    > prompt so the DOS window would wait for a reply from the user.


    Start your program from Visual Studio by pressing ctrl-F5, and the window
    will stay open until you press a key.
    Juha Nieminen, Sep 26, 2012
    #3
  4. On Sep 26, 7:08 am, Brian <> wrote:
    > Paavo Helde <> wrote:
    > > Brian <> wrote in news:159237127370317100.668440bclark-
    > > :


    > >> I'm new to C++ and got a book to learn this programming language.


    what book?

    > >> I downloaded Microsofts Visual Studio C++ express.

    >
    > >> I found that code in the book does not seem to work
    > >> If I enter cout <<  "Hello World";
    > >> then Visual Studio C++ express reports this as an error.


    normally you say what the error is. And for short examples like this
    post the entire program.

    > >> Does Microsofts version of C++ have its own non standard language?


    Microsoft are pretty good at supporting the standard (ok, maybe not
    yet the latest but you aren't dealing with anything esoteric yet). In
    this case the book is at fault.

    > >> Is there a compiler that I can use the accepts the standard C++ language
    > >> that the book has been written for?


    the book is *not* written for standard C++

    <snip>

    > From what I found on the internet it looks like you need to type
    >
    > Std::cout << "Hello World"


    you mean

    std::cout << "Hello World";

    case is important in C++

    > Instead of
    > cout ,<< "Hello World"
    >
    > So it looks like you need to tell Visual Studio C++ that you are enteringa
    > standard C++ command.


    no. That you are using the standard library. And this applies to all
    compliant C++ compilers.

    > I'm not certain what the library "stdafx.h" does or if it's needed just to
    > display the words "Hello World".


    "stdafx.h" is some wierd microsoft stuff. it is not necessary. Hunt
    around for "precompiled headers" and turn them off.

    > When I run this simple code the written for a console, the DOS window
    > quickly pops up.


    ....and more importantly, disappers. This is (what I regard as) a bug
    in Windows. You could get a command window up (a so-called "DOS
    window) and type the name of your .exe. Or (and people *are* going to
    moan about this) add the line system("pause"); to the end of your main
    function() and #include <cstdlib> to the beginning of your program.

    > is there a way for the DOS window to stay on the screen so
    > I can read the words "Hello World" ?   I was thinking of a user input
    > prompt so the DOS window would wait for a reply from the user.
    Nick Keighley, Sep 26, 2012
    #4
  5. Nick Keighley <> wrote:
    > Or (and people *are* going to moan about this)


    And for a good reason. That's a horrible "solution" to a basically
    inexistent problem that teaches bad habits to a beginner programmer
    (namely, using non-portable system-specific code that, on top of all
    that, is also horribly inefficient).

    Just start the program from VS by pressing ctrl-F5, and the window
    will stay open.
    Juha Nieminen, Sep 26, 2012
    #5
  6. Brian

    osmium Guest

    "Brian" wrote:

    > I'm new to C++ and got a book to learn this programming language.
    > I downloaded Microsofts Visual Studio C++ express.
    >
    > I found that code in the book does not seem to work
    > If I enter cout << "Hello World";
    > then Visual Studio C++ express reports this as an error.
    >
    > Does Microsofts version of C++ have its own non standard language?
    > Is there a compiler that I can use the accepts the standard C++ language
    > that the book has been written for?
    > I'm using Windows7 64 bit operating system.


    The code you posted should not work, the book was written in the era before
    there was a standard for C++. You should supplement that book with some
    other source, unless you think it is a great book, it might be best to just
    start over, find a different book.
    osmium, Sep 26, 2012
    #6
  7. Brian

    Brian Guest

    Nick Keighley <> wrote:
    > On Sep 26, 7:08 am, Brian <> wrote:
    >> Paavo Helde <> wrote:
    >>> Brian <> wrote in news:159237127370317100.668440bclark-
    >>> :

    >
    >>>> I'm new to C++ and got a book to learn this programming language.

    >
    > what book?


    The book is called C++ Programming in Easy Steps by Mike McGrath (fourth
    edition of the book).


    >
    >>>> I downloaded Microsofts Visual Studio C++ express.

    >>
    >>>> I found that code in the book does not seem to work
    >>>> If I enter cout << "Hello World";
    >>>> then Visual Studio C++ express reports this as an error.

    >
    > normally you say what the error is. And for short examples like this
    > post the entire program.


    The code in the book is

    #include <iostream>
    using namespace std ;

    Int main()
    {
    cout << "Hello World!" << endl ;
    return 0
    }

    When trying to use this code in Visual Studio C++ express 2010 from memory
    it did not like the line of code 'using namespace std' and it wanted me to
    define cout.


    --
    Regards Brian
    Brian, Sep 26, 2012
    #7
  8. Brian

    Brian Guest

    Paavo Helde <> wrote:
    > Brian <> wrote in
    > news::
    >
    >> The code in the book is
    >>
    >> #include <iostream>
    >> using namespace std ;
    >>
    >> Int main()
    >> {
    >> cout << "Hello World!" << endl ;
    >> return 0
    >> }
    >>
    >> When trying to use this code in Visual Studio C++ express 2010 from
    >> memory it did not like the line of code 'using namespace std' and it
    >> wanted me to define cout.

    >
    > Strange, the "using namespace std;" line is correct in your example, but
    > a couple of other lines are faulty ('Int' -> 'int', 'return 0' -> 'return
    > 0;'. Probably you retyped the example (with different typing mistakes
    > this time!)
    >


    Sorry but it's the auto formatting on my e-mail program that insists on
    starting a word at the start of a line with a capital letter unless I
    notice it and correct for this.
    Visual Studio is good at picking up mistakes such as leaving the semi comma
    off at the end of the line so in the code I used I would have included a
    semi comma character.
    The book I'm using is dated 2011 and is called C++ Programming in easy
    steps.


    > Typically, in C++ you need to concentrate on the first error; if it
    > complains about 'using namespace std;' line then you need to fix the
    > problem; you just can't blindly delete that line without understanding
    > what it does, and hope that the rest of the code somehow miraculously
    > works.
    >
    > And post the error messages next time, no mind-readers here!



    --
    Regards Brian
    Brian, Sep 26, 2012
    #8
  9. On Tuesday, September 25, 2012 6:58:20 PM UTC-7, Brian wrote:
    > I'm new to C++ and got a book to learn this programming language.
    >
    > I downloaded Microsofts Visual Studio C++ express.
    >
    >
    >
    > I found that code in the book does not seem to work
    >
    > If I enter cout << "Hello World";
    >
    > then Visual Studio C++ express reports this as an error.
    >
    >
    >
    > Does Microsofts version of C++ have its own non standard language?
    >
    > Is there a compiler that I can use the accepts the standard C++ language
    >
    > that the book has been written for?
    >
    > I'm using Windows7 64 bit operating system.
    >
    >
    >
    > --
    >
    > Regards Brian

    i think you need to include<iostream.h>
    since that header file has the input and output that can allow the cout as the output and the cin as the input.
    margaret mbuiyu, Sep 26, 2012
    #9
  10. Brian

    Nobody Guest

    On Wed, 26 Sep 2012 07:40:21 -0700, margaret mbuiyu wrote:

    > i think you need to include <iostream.h>


    STL headers with a ".h" suffix are pre-standardisation. You might
    encounter them in legacy code, but new code should use e.g.

    #include <iostream>

    without the suffix.
    Nobody, Sep 26, 2012
    #10
  11. Brian

    Guest

    On Wednesday, September 26, 2012 3:58:20 AM UTC+2, Brian wrote:
    > I'm new to C++ and got a book to learn this programming language.
    >
    > I downloaded Microsofts Visual Studio C++ express.
    >
    >
    >
    > I found that code in the book does not seem to work
    >
    > If I enter cout << "Hello World";
    >
    > then Visual Studio C++ express reports this as an error.
    >
    >
    >
    > Does Microsofts version of C++ have its own non standard language?
    >
    > Is there a compiler that I can use the accepts the standard C++ language
    >
    > that the book has been written for?
    >
    > I'm using Windows7 64 bit operating system.


    You absolutely MUST copy-paste EXACT code you have, and you MUST copy-paste the error message compiler gave you.

    You SHOULD try to come up with a better title. E.g. "could not compile following code" - because, as things stand, cout works just fine for millions of people (you included, you just don't know it yet ;-)).

    Chances are, you made some silly error, but it's very hard to tell what it is if you don't show the code.

    Goran.
    , Sep 27, 2012
    #11
  12. Brian

    Brian Guest

    <> wrote:
    > On Wednesday, September 26, 2012 3:58:20 AM UTC+2, Brian wrote:
    >> I'm new to C++ and got a book to learn this programming language.
    >>
    >> I downloaded Microsofts Visual Studio C++ express.
    >>
    >>
    >>
    >> I found that code in the book does not seem to work
    >>
    >> If I enter cout << "Hello World";
    >>
    >> then Visual Studio C++ express reports this as an error.
    >>
    >>
    >>
    >> Does Microsofts version of C++ have its own non standard language?
    >>
    >> Is there a compiler that I can use the accepts the standard C++ language
    >>
    >> that the book has been written for?
    >>
    >> I'm using Windows7 64 bit operating system.

    >
    > You absolutely MUST copy-paste EXACT code you have, and you MUST
    > copy-paste the error message compiler gave you.
    >
    > You SHOULD try to come up with a better title. E.g. "could not compile
    > following code" - because, as things stand, cout works just fine for
    > millions of people (you included, you just don't know it yet ;-)).
    >
    > Chances are, you made some silly error, but it's very hard to tell what
    > it is if you don't show the code.
    >
    > Goran.


    The book says type in
    cout << "Hello World";

    But when I use Visual Studio C++ 2010 express I need to type
    std::cout << "Hello World";

    Why is that...why can't I just type cout << "Hello World"; ?

    --
    Regards Brian
    Brian, Sep 27, 2012
    #12
  13. "Brian" wrote in message
    news:...
    >
    ><> wrote:
    >> On Wednesday, September 26, 2012 3:58:20 AM UTC+2, Brian wrote:
    >>> I'm new to C++ and got a book to learn this programming language.
    >>>
    >>> I downloaded Microsofts Visual Studio C++ express.
    >>>
    >>>
    >>>
    >>> I found that code in the book does not seem to work
    >>>
    >>> If I enter cout << "Hello World";
    >>>
    >>> then Visual Studio C++ express reports this as an error.
    >>>
    >>>
    >>>
    >>> Does Microsofts version of C++ have its own non standard language?
    >>>
    >>> Is there a compiler that I can use the accepts the standard C++ language
    >>>
    >>> that the book has been written for?
    >>>
    >>> I'm using Windows7 64 bit operating system.

    >>
    >> You absolutely MUST copy-paste EXACT code you have, and you MUST
    >> copy-paste the error message compiler gave you.
    >>
    >> You SHOULD try to come up with a better title. E.g. "could not compile
    >> following code" - because, as things stand, cout works just fine for
    >> millions of people (you included, you just don't know it yet ;-)).
    >>
    >> Chances are, you made some silly error, but it's very hard to tell what
    >> it is if you don't show the code.
    >>
    >> Goran.

    >
    >The book says type in
    >cout << "Hello World";
    >
    >But when I use Visual Studio C++ 2010 express I need to type
    >std::cout << "Hello World";
    >
    >Why is that...why can't I just type cout << "Hello World"; ?
    >


    Because the book is wrong.
    To access an object defined in a namespace, you need to tell the compiler in
    which namespace it can find the object, because other objects with the same
    name are possible in different namespaces.
    Fred Zwarts \(KVI\), Sep 27, 2012
    #13
  14. On Sep 27, 7:45 am, Brian <> wrote:
    > <> wrote:
    > > On Wednesday, September 26, 2012 3:58:20 AM UTC+2, Brian wrote:
    > >> I'm new to C++ and got a book to learn this programming language.


    (Elsewhere in the thread the book was mentioned as _C++ Programming in
    Easy Steps_ (4th ed.) by Mike McGrath)


    > The book says type in
    > cout << "Hello World";



    I found the page you're looking at, page 12, in Amazon's "Look inside
    this book" feature. It does indeed say to type that in, but you'll
    note that above that, it ALSO says to type in

    #include <iostream>
    using namespace std;

    As others have already observed, typing in the EXACT code given in the
    book without any typos or omissions is crucially important. (Not only
    in C++, but for any programming language.)


    > But when I use Visual Studio C++ 2010 express I need to type
    > std::cout << "Hello World";


    Nitpick: Actually, you probably need to type in

    std::cout << "Hello World" << std::endl;

    to see any output -- the 'std::endl' is what actually tells the
    program to dump the text to the screen. It *may* produce output
    without the endl, depending on your platform, but I definitely
    wouldn't count on it.


    > Why is that...why can't I just type cout << "Hello World";  ?


    Well, the shallowest explanation is that you can't do that because you
    didn't type in "using namespace std;" that the book told you to put in
    above that :)

    As for the reasons behind this: You may need to read further in the
    book before fully understanding this, but here goes.

    Most C++ standard library objects, including cout and endl, are in a
    namespace provided by the standard library headers that is called
    "std", rather than the "global namespace". Unless you tell the
    compiler to use a particular namespace, which is what the "using
    namespace std;" does, it only looks up UNqualified object names in the
    global namespace. (Or the current scope, or its parent scope, etc.,
    but you don't have to think about that quite yet.)

    A "qualified" name is something like "std::cout". If you've
    #include'd the necessary headers, using a qualified name will always
    work, whether you've said to use the std namespace or not.

    By the way, "using namespace std;" is seldom desirable in real-world
    code since it pulls in EVERYTHING in the namespace (that's visible
    from current #include directives). It is commonly used in pedagogical
    settings for brevity. But one would usually prefer

    using std::cout;
    using std::endl;

    .... this allows you to use "cout" and "endl" without the std::
    qualification, but without "polluting" your namespace with all the
    other functions, classes, etc. in std. If the Standard Library
    objects were in the global namespace, this selective namespace
    importation wouldn't be possible.

    Hope this helps,

    - Kevin B. McCarty
    Kevin McCarty, Sep 27, 2012
    #14
  15. Brian

    Brian Guest

    Kevin McCarty <> wrote:
    > On Sep 27, 7:45 am, Brian <> wrote:
    >> <> wrote:
    >>> On Wednesday, September 26, 2012 3:58:20 AM UTC+2, Brian wrote:
    >>>> I'm new to C++ and got a book to learn this programming language.

    >
    > (Elsewhere in the thread the book was mentioned as _C++ Programming in
    > Easy Steps_ (4th ed.) by Mike McGrath)
    >
    >
    >> The book says type in
    >> cout << "Hello World";

    >
    >
    > I found the page you're looking at, page 12, in Amazon's "Look inside
    > this book" feature. It does indeed say to type that in, but you'll
    > note that above that, it ALSO says to type in
    >
    > #include <iostream>
    > using namespace std;
    >
    > As others have already observed, typing in the EXACT code given in the
    > book without any typos or omissions is crucially important. (Not only
    > in C++, but for any programming language.)
    >
    >
    >> But when I use Visual Studio C++ 2010 express I need to type
    >> std::cout << "Hello World";

    >
    > Nitpick: Actually, you probably need to type in
    >
    > std::cout << "Hello World" << std::endl;
    >
    > to see any output -- the 'std::endl' is what actually tells the
    > program to dump the text to the screen. It *may* produce output
    > without the endl, depending on your platform, but I definitely
    > wouldn't count on it.
    >
    >
    >> Why is that...why can't I just type cout << "Hello World"; ?

    >
    > Well, the shallowest explanation is that you can't do that because you
    > didn't type in "using namespace std;" that the book told you to put in
    > above that :)
    >
    > As for the reasons behind this: You may need to read further in the
    > book before fully understanding this, but here goes.
    >
    > Most C++ standard library objects, including cout and endl, are in a
    > namespace provided by the standard library headers that is called
    > "std", rather than the "global namespace". Unless you tell the
    > compiler to use a particular namespace, which is what the "using
    > namespace std;" does, it only looks up UNqualified object names in the
    > global namespace. (Or the current scope, or its parent scope, etc.,
    > but you don't have to think about that quite yet.)
    >
    > A "qualified" name is something like "std::cout". If you've
    > #include'd the necessary headers, using a qualified name will always
    > work, whether you've said to use the std namespace or not.
    >
    > By the way, "using namespace std;" is seldom desirable in real-world
    > code since it pulls in EVERYTHING in the namespace (that's visible
    > from current #include directives). It is commonly used in pedagogical
    > settings for brevity. But one would usually prefer
    >
    > using std::cout;
    > using std::endl;
    >
    > ... this allows you to use "cout" and "endl" without the std::
    > qualification, but without "polluting" your namespace with all the
    > other functions, classes, etc. in std. If the Standard Library
    > objects were in the global namespace, this selective namespace
    > importation wouldn't be possible.
    >
    > Hope this helps,
    >
    > - Kevin B. McCarty


    Thanks Kevin for taking the time to look more closely at the problem.
    Originally I typed in all the code including the using namespace std;
    but Visual Studio C++ did not like the line 'using namespace std;' I will
    in future write down the error message when something goes wrong.

    --
    Regards Brian
    Brian, Sep 28, 2012
    #15
  16. Kevin McCarty <> wrote:
    > It is commonly used in pedagogical settings for brevity.


    Which is highly ironic given that

    "using namespace std; cout endl"

    is longer than

    "std::cout std::endl"

    Books should not teach a hatred of the std:: prefix, but on the contrary,
    they should encourage its use. In fact, it would be better if the entire
    "using" keyword would be left to a much, much later chapter on advanced
    topics. Giving it in the very first piece of code ever to be shown to the
    beginner programmer is a disservice.
    Juha Nieminen, Sep 28, 2012
    #16
  17. Brian

    osmium Guest

    "Juha Nieminen"

    > Kevin McCarty <> wrote:
    >> It is commonly used in pedagogical settings for brevity.

    >
    > Which is highly ironic given that
    >
    > "using namespace std; cout endl"
    >
    > is longer than
    >
    > "std::cout std::endl"


    I don't see anything that looks ironic. The first form is typed once, the
    second form is typed for each and every occurrence of one of the calls.
    osmium, Sep 28, 2012
    #17
  18. On Sep 27, 9:05 pm, Brian <> wrote:

    > Thanks Kevin for taking the time to look more closely at the problem.
    > Originally I typed in all the code including the   using namespace std;
    > but Visual Studio C++ did not like the line 'using namespace std;' I will
    > in future write down the error message when something goes wrong.
    >
    > --
    > Regards Brian


    All I can say is that it works for me with VC++ 10 (Visual Studio
    2010), see my copy-and-paste from a terminal window below.

    I still believe there was a typo on your first try... but it's hard to
    say for certain, as I don't think you ever provided your entire piece
    of failing code. Only hand-copied excerpts which had their own
    additional typos :)

    One more time, when asking on Usenet (or anywhere else) about compiler
    error messages, do NOT just hand-copy them down; you need to COPY-AND-
    PASTE the error messages and also your entire piece of code into your
    post, or it is almost certain that there will be typos that prevent
    others from diagnosing the actual problem.

    Good luck, and sorry I couldn't help any further,
    - Kevin B. McCarty


    D:\users\kevin> type hw.cpp
    #include <iostream>
    using namespace std;

    int main()
    {
    cout << "Hello world!" << endl;
    return 0;
    }

    D:\users\kevin> cl /EHsc hw.cpp
    Microsoft (R) C/C++ Optimizing Compiler Version 16.00.40219.01 for x64
    Copyright (C) Microsoft Corporation. All rights reserved.

    hw.cpp
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation. All rights reserved.

    /out:hw.exe
    hw.obj

    D:\users\kevin> hw.exe
    Hello world!
    Kevin McCarty, Sep 28, 2012
    #18
  19. Brian

    ArbolOne Guest

    No, no new really, MSC++ follows the standard and then adds more stuff.
    Ahh, and by the way the code should read

    void someClass::display{ // just as a test method
    std::cout << "What ever you wanna say" << std::endl;
    }
    ArbolOne, Sep 28, 2012
    #19
  20. On 9/28/2012 4:34 PM, ArbolOne wrote:
    > No, no new really, MSC++ follows the standard and then adds more stuff.
    > Ahh, and by the way the code should read
    >
    > void someClass::display{ // just as a test method


    You probably forgot the argument list and the parens:

    void someClass::display () { // just as a test method
    // ^^^^
    // This stuff

    without them it's not going to compile, I'm afraid.

    > std::cout << "What ever you wanna say" << std::endl;
    > }
    >


    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 28, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,636
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,394
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    309
    Howard
    Sep 15, 2005
  4. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    463
  5. Hendrik Schober

    Re: cout vs std::cout

    Hendrik Schober, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    426
    Jerry Coffin
    Oct 7, 2008
Loading...

Share This Page