cout does not work

B

Brian

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.
 
B

Brian

Paavo Helde said:
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.
 
J

Juha Nieminen

Brian said:
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.
 
N

Nick Keighley

what book?

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

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.

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

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
 
J

Juha Nieminen

Nick Keighley said:
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.
 
O

osmium

Brian said:
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.
 
B

Brian

Nick Keighley said:
what book?

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

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.
 
B

Brian

Paavo Helde said:
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.
 
M

margaret mbuiyu

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.
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.
 
N

Nobody

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.
 
G

goran.pusic

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.
 
B

Brian

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"; ?
 
F

Fred Zwarts \(KVI\)

"Brian" wrote in message
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.
 
K

Kevin McCarty

(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
 
B

Brian

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




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



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.



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.
 
J

Juha Nieminen

Kevin McCarty said:
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.
 
O

osmium

"Juha Nieminen"
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.
 
K

Kevin 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.

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

ArbolOne

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;
}
 
V

Victor Bazarov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top