Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cout does not work

153 views
Skip to first unread message

Brian

unread,
Sep 25, 2012, 9:58:19 PM9/25/12
to
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

Paavo Helde

unread,
Sep 26, 2012, 12:54:29 AM9/26/12
to
Brian <bcl...@es.co.nz> wrote in news:159237127370317100.668440bclark-
es.c...@free.teranews.com:
See http://www.parashift.com/c++-faq/posting-code.html

Visual Studio is generally OK, maybe the problem is with your book.

Brian

unread,
Sep 26, 2012, 2:08:32 AM9/26/12
to
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

Juha Nieminen

unread,
Sep 26, 2012, 3:54:22 AM9/26/12
to
Brian <bcl...@es.co.nz> 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.

Nick Keighley

unread,
Sep 26, 2012, 5:07:33 AM9/26/12
to
On Sep 26, 7:08 am, Brian <bcl...@es.co.nz> wrote:
> Paavo Helde <myfirstn...@osa.pri.ee> wrote:
> > Brian <bcl...@es.co.nz> wrote in news:159237127370317100.668440bclark-
> > es.co...@free.teranews.com:

> >> 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 entering a
> 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.

Juha Nieminen

unread,
Sep 26, 2012, 6:38:45 AM9/26/12
to
Nick Keighley <nick_keigh...@hotmail.com> 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.

osmium

unread,
Sep 26, 2012, 8:22:44 AM9/26/12
to
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.



Brian

unread,
Sep 26, 2012, 8:28:55 AM9/26/12
to
Nick Keighley <nick_keigh...@hotmail.com> wrote:
> On Sep 26, 7:08 am, Brian <bcl...@es.co.nz> wrote:
>> Paavo Helde <myfirstn...@osa.pri.ee> wrote:
>>> Brian <bcl...@es.co.nz> wrote in news:159237127370317100.668440bclark-
>>> es.co...@free.teranews.com:
>
>>>> 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

Paavo Helde

unread,
Sep 26, 2012, 8:40:58 AM9/26/12
to
Brian <bcl...@es.co.nz> wrote in
news:1168380555370354716....@free.teranews.com:

> 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!)

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!

Brian

unread,
Sep 26, 2012, 9:39:54 AM9/26/12
to
Paavo Helde <myfir...@osa.pri.ee> wrote:
> Brian <bcl...@es.co.nz> wrote in
> news:1168380555370354716....@free.teranews.com:
>
>> 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

Paavo Helde

unread,
Sep 26, 2012, 9:54:13 AM9/26/12
to
Brian <bcl...@es.co.nz> wrote in
news:1677980162370359222....@free.teranews.com:
> 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.

I think you still do not get it. Please post copy-pasted code AND copy-
pasted error messages. I do not know of any program which changes capitals
in pasted text automatically (and certainly there is none which
automatically deletes semicolons). Otherwise, how can we be sure that the
rest of lines are the same than in the actual code you have trouble with?

Cheers
Paavo

margaret mbuiyu

unread,
Sep 26, 2012, 10:40:21 AM9/26/12
to
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.


Nobody

unread,
Sep 26, 2012, 6:14:45 PM9/26/12
to
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.

goran...@gmail.com

unread,
Sep 27, 2012, 6:27:16 AM9/27/12
to
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.

Brian

unread,
Sep 27, 2012, 10:45:12 AM9/27/12
to
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

Fred Zwarts (KVI)

unread,
Sep 27, 2012, 11:25:37 AM9/27/12
to
"Brian" wrote in message
news:1146657349370449667....@free.teranews.com...
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.

Kevin McCarty

unread,
Sep 27, 2012, 11:35:54 AM9/27/12
to
On Sep 27, 7:45 am, Brian <bcl...@es.co.nz> wrote:
> <goran.pu...@gmail.com> 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

Brian

unread,
Sep 28, 2012, 12:05:36 AM9/28/12
to
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

Juha Nieminen

unread,
Sep 28, 2012, 2:35:04 AM9/28/12
to
Kevin McCarty <kmcc...@gmail.com> 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.

osmium

unread,
Sep 28, 2012, 10:41:14 AM9/28/12
to
"Juha Nieminen"

> Kevin McCarty <kmcc...@gmail.com> 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.


Kevin McCarty

unread,
Sep 28, 2012, 12:42:32 PM9/28/12
to
On Sep 27, 9:05 pm, Brian <bcl...@es.co.nz> 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!

ArbolOne

unread,
Sep 28, 2012, 4:34:38 PM9/28/12
to
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;
}

Victor Bazarov

unread,
Sep 28, 2012, 4:36:43 PM9/28/12
to
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

Brian

unread,
Sep 30, 2012, 1:35:54 AM9/30/12
to
When I get a spare moment I hope to try entering the code again but this
time I will write down any error messages.
I tend to send e-mail on my iPad so its not possible to cut and paste
between two computer and I have the newsgroups setup on my iPad. But I
could e-mail it to myself and receive the e-mail on my iPad then cut and
copy it to this newsgroup.
The reason I use a iPad is that its ready to use as soon as you switch it
on and its very portable, light in weight.and not too big.

--
Regards Brians

Geoff

unread,
Oct 7, 2012, 2:57:46 AM10/7/12
to
On Wed, 26 Sep 2012 07:54:22 +0000 (UTC), Juha Nieminen
<nos...@thanks.invalid> wrote:

>Start your program from Visual Studio by pressing ctrl-F5, and the window
>will stay open until you press a key.

That's a nice feature but ctrl-F5 is "start without debugging" while
F5 is "start debugging".

The preferred method for most uses of the debugger is to set a
breakpoint at the expected program termination point. This will keep
the debuggee window open until the process is terminated by the
debugger or you execute past the breakpoint. The obvious disadvantage
of breakpoints is that you have to place them at all the exits if your
program has more than one.

Juha Nieminen

unread,
Oct 7, 2012, 6:25:01 AM10/7/12
to
Geoff <ge...@invalid.invalid> wrote:
> On Wed, 26 Sep 2012 07:54:22 +0000 (UTC), Juha Nieminen
> <nos...@thanks.invalid> wrote:
>
>>Start your program from Visual Studio by pressing ctrl-F5, and the window
>>will stay open until you press a key.
>
> That's a nice feature but ctrl-F5 is "start without debugging" while
> F5 is "start debugging".

I really can't understand what's Microsoft's rationale behind having
the pause only when you start in non-debug mode, but you should be
complaining to them...

Robert Wessel

unread,
Oct 7, 2012, 12:08:07 PM10/7/12
to
On Sat, 06 Oct 2012 23:57:46 -0700, Geoff <ge...@invalid.invalid>
wrote:
Still OT, but...

If you set a breakpoint at the beginning of the program*, and then use
ctrl-F5, you'll be able to debug from that point on, and the console
window will stay open at the end. The one downside is that it's tough
to debug things that happen before the first line of main() (such as
the execution of constructors for global objects).

The application does needed to be linked with /SUBSYSTEM:CONSOLE
(which should be there if you started the project as a console
application, but IIRC, the default has changed if you start with an
*empty* project).

This is an annoying property of VS, and has been for many years. The
reason for the difference is that VS runs a "without debugging"
application differently - it actually creates a batch file to run the
program, and puts a "pause" at the end of that, and then runs the
batch file - "with debugging" starts the program directly under the
control of the debugger. Still this is annoying enough that MS should
have found a way to fix it, even though it might require as OS change.


*Alternatively put a DebugBreak() call at the beginning of main(), and
you don't have to remember to manually set the breakpoint there.

Tobias Müller

unread,
Oct 8, 2012, 2:25:28 AM10/8/12
to
Robert Wessel <robert...@yahoo.com> wrote:
> If you set a breakpoint at the beginning of the program*, and then use
> ctrl-F5, you'll be able to debug from that point on, and the console
> window will stay open at the end.

If you start with ctrl-F5, no debugger will be attached and all breakpoints
are simply ignored.
The only way to debug is in that case to manually attach the debugger to
the process.

> The one downside is that it's tough
> to debug things that happen before the first line of main() (such as
> the execution of constructors for global objects).
>
> The application does needed to be linked with /SUBSYSTEM:CONSOLE
> (which should be there if you started the project as a console
> application, but IIRC, the default has changed if you start with an
> *empty* project).

If you compile/link without /SUBSYSTEM:CONSOLE, you don't even have a
console. If you don't have one, you shouldn't care much whether it will be
closed or not.

> This is an annoying property of VS, and has been for many years. The
> reason for the difference is that VS runs a "without debugging"
> application differently - it actually creates a batch file to run the
> program, and puts a "pause" at the end of that, and then runs the
> batch file - "with debugging" starts the program directly under the
> control of the debugger. Still this is annoying enough that MS should
> have found a way to fix it, even though it might require as OS change.

Put a breakpoint on the closing brace of main(), and the console will stay
open. Where's the problem?

> *Alternatively put a DebugBreak() call at the beginning of main(), and
> you don't have to remember to manually set the breakpoint there.

Which I'd consider even worse than putting getc() at the end of main().

Tobi
0 new messages