stoopid begineer with a question

K

knilges

I am trying to build a Hello World program in C++. I am using the c++ or
g++ complier in cygwin. My mangled code looks like this,

#include <iostream>

int main ()
{
cout << "Hello World";
return;
}

It looks like my example but it isn't working.

the compiler for g++ tells me the following:

hellotest.cpp: In function `int main()':
hellotest.cpp:5: error: `cout' undeclared (first use this function)
hellotest.cpp:5: error: (Each undeclared identifier is reported only once
for each function it appears in.)
hellotest.cpp:6: error: return-statement with no value, in function
returning 'int'

When I run it with an older complier Borland 4.5 it tells me that it can't
read the input file helloworld.rc.

What am I doing wrong?
 
R

roberts.noah

knilges said:
I am trying to build a Hello World program in C++. I am using the c++ or
g++ complier in cygwin. My mangled code looks like this,

#include <iostream>

int main ()
{
cout << "Hello World";
return;
}

int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
It looks like my example but it isn't working.

the compiler for g++ tells me the following:

hellotest.cpp: In function `int main()':
hellotest.cpp:5: error: `cout' undeclared (first use this function)
hellotest.cpp:5: error: (Each undeclared identifier is reported only once
for each function it appears in.)
hellotest.cpp:6: error: return-statement with no value, in function
returning 'int'

When I run it with an older complier Borland 4.5 it tells me that it can't
read the input file helloworld.rc.

That is some sort of compiler dependent file.
 
T

Thomas Tutone

knilges said:
I am trying to build a Hello World program in C++. I am using the c++ or
g++ complier in cygwin. My mangled code looks like this,

#include <iostream>

int main ()
{
cout << "Hello World";

replace the above line with:

std::cout << "Hello World";

and your program will compile fine.

Delete the above line entirely, or replace it with:

return 0;

Best regards,

Tom
 
M

Mike Smith

int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}

Or just leave out the return - a main() with no return is assumed to
return EXIT_SUCCESS.
 
D

David Harmon

On 18 Jan 2006 07:57:58 -0800 in comp.lang.c++,
(e-mail address removed) wrote,
std::cout << "Hello World" << std::endl;

endl is uncalled for there. Please don't teach bad habits.
If you want to add a newline, it should be:

std::cout << "Hello World\n";
 
R

roberts.noah

David said:
On 18 Jan 2006 07:57:58 -0800 in comp.lang.c++,
(e-mail address removed) wrote,

endl is uncalled for there. Please don't teach bad habits.
If you want to add a newline, it should be:

std::cout << "Hello World\n";

Since std::cout may very well be buffered it is actually prudent to
pass std::endl instead of '\n'. In this *particular* case, since the
program exits immediately following the operation it is "unneccisary"
but it is a GOOD habit to get into since in anything more complex than
a hello world program is going to do more and you should flush your
outputs when you want them to be sent.

So, to each his own. I'll answer questions the way I see fit and
people can learn, or not, what they want.

Thanks anyway.
 
M

Mike Wahler

Since std::cout may very well be buffered it is actually prudent to
pass std::endl instead of '\n'. In this *particular* case, since the
program exits immediately following the operation it is "unneccisary"
but it is a GOOD habit

IMO a 'good' habit would be something that is
always 'good' in general (e.g. use consistent
indentation style). But imo 'proper' use of
'endl' depends much upon context. Because of
what it does, using it needlessly can unnecessarily
degrade performance. (i/o is typically the slowest
part of a system -- that's why buffering was invented).
to get into since in anything more complex than
a hello world program is going to do more and you should flush your
outputs when you want them to be sent.

But it's often the case that 'visible' output is not needed
at every occurence of '\n'.

So, to each his own. I'll answer questions the way I see fit and
people can learn, or not, what they want.

And others will insert their opinions as they see fit. :)

FWIW I've still never had the need to use 'endl' with
output streams.

-Mike
 
M

Mike Wahler

Well it seems some people feel I need permission to do so.

Really? I haven't seen any indication of that. If someone
responds to your post with e.g. "don't say that", that simply
means they disagree, not that they're demanding you ask permission
to say it. (Civilly expressed) disagreements often lead to
useful discussion. Discussion sheds light on both sides of an
issue, and allows observers to draw their own conclusions.
I think that is especially useful for the novice.

-Mike
 
A

adrian suri

Hi

try this instead, you missed out the name space declaration
//hello.cpp

#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World" <<endl;

return 0;
}

compiles fine under watcom 1.4 under OS/2 4.52

regards
 
A

adrian suri

hi

Hi

try this instead, you missed out the name space declaration
//hello.cpp

#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World" <<endl;

return 0;
}

compiles fine under watcom 1.4 under OS/2 4.52

regards

Adrian
 
G

Gavin Deane

Daniel said:

As I recall, Herb Sutter disagrees with the faq on this point.[/QUOTE]

Can you cite any reference for that? An online source or a book? I'd be
quite surprised to see Herb Sutter generally recommending using
directives (as opposed to using declarations). If he does disagree with
the FAQ I would be interested to read about it.

Gavin Deane
 
D

Daniel T.

"Gavin Deane said:
Can you cite any reference for that?

C/C++ Users Journal April 2004 "Using Me"
I'd be
quite surprised to see Herb Sutter generally recommending using
directives (as opposed to using declarations). If he does disagree with
the FAQ I would be interested to read about it.

I thought he had it online, because he originally recommended not doing
it (just like he originally recommended using deque rather than vector
as the default container) then changed his mind.

[Looks some more...] I found it, look at message 15 of this thread:
<http://tinyurl.com/a79an>
 
G

Gavin Deane

Daniel said:
Gavin Deane said:
Can you cite any reference for that?

C/C++ Users Journal April 2004 "Using Me"
I'd be
quite surprised to see Herb Sutter generally recommending using
directives (as opposed to using declarations). If he does disagree with
the FAQ I would be interested to read about it.

I thought he had it online, because he originally recommended not doing
it (just like he originally recommended using deque rather than vector
as the default container) then changed his mind.

[Looks some more...] I found it, look at message 15 of this thread:
<http://tinyurl.com/a79an>

Thanks for digging that out.

I'm actually a little surprised at some of his original recommendations
(the one's where he's now changed his mind). I don't think I'd bother
with using declarations in toy programs. And if I was migrating a code
base with the help of using directives, I'm not sure I'd necessarily
bother replacing them with appropriate using declarations at the end of
the process.

His advice seems to boil down to "Use the tools the language provides
you to make your life easier". Makes sense to me, as long as you
understand the risks inherent in those tools as well.

Gavin Deane
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top