By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,872 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,872 IT Pros & Developers. It's quick & easy.

endl error (extreme novice)

P: n/a
I am working on my second program ever, so bear with me.
When I try to compile it, I get two error messages(sort of 3):

In function `int main()':
line 6: `endl' undeclared (first use this function)
line 8 `end' undeclared (first use this function)

I'm using a compiler called Dev-C++, so if anyone knows a better one, I
am open to recommendations.
Thank you,

Here is my current program:

#include <iostream>
int main()
{
int x = 5;
int y = 7;
std::cout << endl;
std::cout << x + y << " " << x * y;
std::cout << end;
return 0;
}

Sep 17 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a
#include <iostream>
int main()
{
int x = 5;
int y = 7;
std::cout << endl;
std::cout << x + y << " " << x * y;
std::cout << end;
return 0;
}
endl is(just as cout) within the scope of std.
so you have to put std:: before it.
you can alternatively put this line after #include <iostream>
using namespace std;

it will declare "std" as the "standard" namespace
Sep 17 '06 #2

P: n/a

Jd******@gmail.com wrote:
I am working on my second program ever, so bear with me.
When I try to compile it, I get two error messages(sort of 3):

In function `int main()':
line 6: `endl' undeclared (first use this function)
line 8 `end' undeclared (first use this function)

I'm using a compiler called Dev-C++, so if anyone knows a better one, I
am open to recommendations.
Dev-C++ is the environment. As I recall, the compiler it uses is g++
and there's nothing wrong with it.
Thank you,
>Here is my current program:

#include <iostream>
Do you know what that line above does?
int main()
{
int x = 5;
int y = 7;
std::cout << endl;
Do you know why you have written std:: in front of cout. Do the same
with endl, i.e. change endl to std::endl, and your problem will go
away, but do you know why?
std::cout << x + y << " " << x * y;
As a side issue, are you sure that line is correct? When you get the
program working, is the output what you intended?
std::cout << end;
endl has become end. I expect you didn't mean that.
return 0;
}
Where are you learning C++ from that doesn't explain this? A book? Or a
website? Because the error in your code, correctly putting std:: in
front of one name (cout) but incorrectly leaving it off another name
(endl), is a strange one for a book or a decent web tutorial (of which
there are very few) to make.

Have you got any programming experience in languages other than C++? If
so, then http://www.acceleratedcpp.com/ is a superb book to teach you
C++ programming. But it does presume some previous, non-C++,
experience.

Gavin Deane

Sep 17 '06 #3

P: n/a

Jd******@gmail.com wrote:
I am working on my second program ever, so bear with me.
When I try to compile it, I get two error messages(sort of 3):

In function `int main()':
line 6: `endl' undeclared (first use this function)
line 8 `end' undeclared (first use this function)

I'm using a compiler called Dev-C++, so if anyone knows a better one, I
am open to recommendations.
Dev-C++ is the environment. As I recall, the compiler it uses is g++
and there's nothing wrong with it.
Thank you,
>Here is my current program:

#include <iostream>
Do you know what that line above does?
int main()
{
int x = 5;
int y = 7;
std::cout << endl;
Do you know why you have written std:: in front of cout. Do the same
with endl, i.e. change endl to std::endl, and your problem will go
away, but do you know why?
std::cout << x + y << " " << x * y;
As a side issue, are you sure that line is correct? When you get the
program working, is the output what you intended?
std::cout << end;
endl has become end. I expect you didn't mean that.
return 0;
}
Where are you learning C++ from that doesn't explain this? A book? Or a
website? Because the error in your code, correctly putting std:: in
front of one name (cout) but incorrectly leaving it off another name
(endl), is a strange one for a book or a decent web tutorial (of which
there are very few) to make.

Have you got any programming experience in languages other than C++? If
so, then http://www.acceleratedcpp.com/ is a superb book to teach you
C++ programming. But it does presume some previous, non-C++,
experience.

Gavin Deane

Sep 17 '06 #4

P: n/a
Yeah, I am doing all of this from a book.
This is an exercize in the book. We were supposed to guess what the
program did by looking at the programming, and then compile it and see
the output.
I copied the program exactly as shown in the book, which is a little
annoying, since it didnt work. Now though, the book has showed me how
to use namespace (using namespace std;) so I can save time like that.
I think its amazing that a book left such an egregious error in there,
and they kept leaving std:: off of the endl, before we learned
namespace or "using std::cout;"
Thanks for your help.
-Jason

Sep 17 '06 #5

P: n/a
On 17 Sep 2006 13:04:39 -0700 in comp.lang.c++, Jd******@gmail.com
wrote,
>I think its amazing that a book left such an egregious error in there,
and they kept leaving std:: off of the endl, before we learned
namespace or "using std::cout;"
I think it is a bigger error, telling you to use endl at all when
you have no need for it. In ordinary circumstances you should use
"\n" or '\n' for the end of output lines.

endl is a special purpose manipulator having to do with forcing
output to occur immediately when the default stream behavior isn't
good enough. The default stream behavior is almost always exactly
right in this regard. endl should be used rarely.

All in my opinion, of course.

Sep 17 '06 #6

P: n/a

Jd******@gmail.com wrote:
Yeah, I am doing all of this from a book.
What book was it?
This is an exercize in the book. We were supposed to guess what the
program did by looking at the programming, and then compile it and see
the output.
I copied the program exactly as shown in the book, which is a little
annoying, since it didnt work. Now though, the book has showed me how
to use namespace (using namespace std;) so I can save time like that.
I think its amazing that a book left such an egregious error in there,
So do I. Does the book have a website? Some do, where, amongst other
things, they list errata.

Gavin Deane

Sep 17 '06 #7

P: n/a

David Harmon wrote:
On 17 Sep 2006 13:04:39 -0700 in comp.lang.c++, Jd******@gmail.com
wrote,
I think its amazing that a book left such an egregious error in there,
and they kept leaving std:: off of the endl, before we learned
namespace or "using std::cout;"

I think it is a bigger error, telling you to use endl at all when
you have no need for it. In ordinary circumstances you should use
"\n" or '\n' for the end of output lines.

endl is a special purpose manipulator having to do with forcing
output to occur immediately when the default stream behavior isn't
good enough. The default stream behavior is almost always exactly
right in this regard. endl should be used rarely.

All in my opinion, of course.
FWIW I agree totally. The only reason I didn't point that out to the OP
was that, facing a book that manages to produce example code with an
omission like he posted, I thought he had far more fundamental
difficulties to overcome without having to understand that endl is
ordinarily not required.

Gavin Deane

Sep 17 '06 #8

P: n/a
The book is called "Sams Teach Yourself C++ in 21 Days, Fifth Edition"
Its required for a class that I start at community college in a couple
weeks. (I'm in High School, and my school has a bad programming
course, and it didnt fit into my schedule.)
I found the website, and it is
http://libertyassociates.com/pages/Books.htm
The site also has the "source code" from the book( just found this out)
http://libertyassociates.com/pages/f...PP5eSource.zip
The one I was having trouble with was in day 1, Ex0101.
I checked out the source file from the website, and it actually had the
correct use of std::endl;
I think its rather funny that the book has incorrect usage in there,
and the script copied directly from the book does not work. I'm going
along with the book, but ignoring a lot of its frivilous text, such as
writing std::cout 30 times, and then doing the program with using
namespace std;

The author make pompous little comments here and there, like "There is
an endless debate about how to pronounce char. (he goes on to blather)
Clearly, car is the correct pronunciation, because that is how I say
it"

Thanks for the help guys.
I'm well on my way now (Day 4)
-Jason

Sep 17 '06 #9

P: n/a

Jd******@gmail.com wrote:
I am working on my second program ever, so bear with me.
When I try to compile it, I get two error messages(sort of 3):

In function `int main()':
line 6: `endl' undeclared (first use this function)
line 8 `end' undeclared (first use this function)

I'm using a compiler called Dev-C++, so if anyone knows a better one, I
am open to recommendations.
Thank you,

Here is my current program:

#include <iostream>
int main()
{
int x = 5;
int y = 7;
std::cout << endl;
std::cout << x + y << " " << x * y;
std::cout << end;
return 0;
}
I have some unsolicited advice unrelated to your original question
(which has now been answered).

On your dev c++ compiler, make the compiler setting -pedantic -Wall.
That way, you will get extra warnings which are essential for a
beginner.

For example, if you write if (x=0) y=5; you will get a warning even
though it is legal code. The default setting would not give a warning
even though this code is usually a mistake.

Some people don't like the extra warnings but it's good for a beginner.

I'm learning from C++ Primer and I recommend it.

Paul Epstein

Sep 17 '06 #10

P: n/a
On 17 Sep 2006 15:25:14 -0700 in comp.lang.c++, Jd******@gmail.com
wrote,
>The book is called "Sams Teach Yourself C++ in 21 Days, Fifth Edition"
Haven't got that one. On the other hand, the caveat from Stroustrup
chapter 1 is:

It is generally in poor taste to dump every name from a
namespace into the global namespace. However, to keep short
the program fragments used to illustrate language and library
features, I omit repetitive #includes and std:: qualifications.

You have got to get used to stuff like that quickly in order to
finish by 21 days! I wonder how my book "A new millennium and still
learning C++" would sell.
Sep 17 '06 #11

P: n/a

David Harmon wrote:
On 17 Sep 2006 13:04:39 -0700 in comp.lang.c++, Jd******@gmail.com
wrote,
I think its amazing that a book left such an egregious error in there,
and they kept leaving std:: off of the endl, before we learned
namespace or "using std::cout;"

I think it is a bigger error, telling you to use endl at all when
you have no need for it. In ordinary circumstances you should use
"\n" or '\n' for the end of output lines.

endl is a special purpose manipulator having to do with forcing
output to occur immediately when the default stream behavior isn't
good enough. The default stream behavior is almost always exactly
right in this regard. endl should be used rarely.

All in my opinion, of course.
I don't know enough to take a position but...
std::endl is extremely common in elementary textbooks. Why do they use
endl so often? Are they all wrong?

I once wrote a program where all my debugging messages used \n instead
of endl. The result was that some of the debugging messages never
appeared on the ouput (which was cout -- the screen) because I never
flushed the buffer.

Paul Epstein

Sep 18 '06 #12

P: n/a

pa**********@att.net wrote:
I don't know enough to take a position but...
std::endl is extremely common in elementary textbooks. Why do they use
endl so often? Are they all wrong?
Usually yes, in the context presented. I am only guessing, but I
imagine one of the reasons they might do it is a misconception that
std::endl is the "C++ way" of outputting a newline. That misconception
was certainly taught to me in a very introductory course I took.
I once wrote a program where all my debugging messages used \n instead
of endl. The result was that some of the debugging messages never
appeared on the ouput (which was cout -- the screen) because I never
flushed the buffer.
Without seeing the code, it's impossible to know whether the behaviour
you observed was as it should be.

Gavin Deane

Sep 18 '06 #13

P: n/a
David Harmon <ba*@example.invalidwrote:
>I think it is a bigger error, telling you to use endl at all when
you have no need for it. In ordinary circumstances you should use
"\n" or '\n' for the end of output lines.
>endl is a special purpose manipulator having to do with forcing
output to occur immediately when the default stream behavior isn't
good enough. The default stream behavior is almost always exactly
right in this regard. endl should be used rarely.
>All in my opinion, of course.
Thanks for this information, of which I was unaware. Possibly
unrelatedly, I found recently that "cout << "string" followed by
system("cat file") did not output data in the expected order.
I had to do a fflush() before the system(). So it seems maybe
the creators of cout / endl had some obscure ideas regarding
flushing output.

Steve
Sep 18 '06 #14

P: n/a
<pa**********@att.netwrote:
>For example, if you write if (x=0) y=5; you will get a warning even
though it is legal code.
g++ gives me a syntax error on that.

S.
Sep 19 '06 #15

P: n/a

Steve Pope wrote:
<pa**********@att.netwrote:
For example, if you write if (x=0) y=5; you will get a warning even
though it is legal code.

g++ gives me a syntax error on that.
Do you get a syntax error with this?

int main()
{
int x = 0;
int y = 0;
if (x=0) y=5;
}

Gavin Deane

Sep 19 '06 #16

P: n/a
Steve Pope posted:
><pa**********@att.netwrote:
>>For example, if you write if (x=0) y=5; you will get a warning even
though it is legal code.

g++ gives me a syntax error on that.

Yes, but that's a bad example, because "y=5" will NEVER be executed. Try
something like:

void Func(int const a, int const b)
{
if (a = b) b = 5;
}

Of course, that should be written as:

a = b;

if(b) b = 5;
--

Frederick Gotham
Sep 19 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.