endl error

Recommended Posts

I'm working on an exercise program in my C++ book. Here is the source code: // Listing 2.2 using cout #include <iostream> int main() { std::cout << "Hello there.\n"; std::cout << "Here is 5: " << 5 << "\n"; std::cout << "The manipulator endl writes a new line to the screen."; std::cout << endl; std::cout << "Here is a very big number:\t" << 7000 << endl; std::cout << "Here is the sum of 8 and 5:\t" << 8 + 5 << endl; std::cout << "Here's a fraction:\t\t" << (float) 5/8 << endl; std::cout << "And a very very big number:\t"; std::cout << (double) 7000 * 7000 << endl; std::cout << "Don't forget to replace Jesse Liberty with your name...\n"; std::cout << "Jesse Liberty is a C++ Programmer!\n"; return 0; } However, I keep getting this error: "9 C:\Documents and Settings\Joey\My Documents\cpp\exercise2.cpp endl' undeclared (first use this function) (Each undeclared identifier is reported only once for each function it appears in.) " The book doesn't tell me anything about an error. Line's 8 and 9 are confusing though, so I deleted them, but the same problem with endl; came up for the previous line. Any help would be greatly appreciated, thanks.

Share on other sites
You should use either std::endl or put 'using namespace std' at the top of your code after the header include and drop the std:: prefixes all together.

Share on other sites
endl is in namespace std, just like cout, so it's called std::endl

Corrected code:
#include <iostream>int main(){std::cout << "Hello there.\n";std::cout << "Here is 5: " << 5 << "\n";std::cout << "The manipulator endl writes a new line to the screen.";std::cout << std::endl;std::cout << "Here is a very big number:\t" << 7000 << std::endl;std::cout << "Here is the sum of 8 and 5:\t" << 8 + 5 << std::endl;std::cout << "Here's a fraction:\t\t" << (float) 5/8 << std::endl;std::cout << "And a very very big number:\t";std::cout << (double) 7000 * 7000 << std::endl;std::cout << "Don't forget to replace Jesse Liberty with your name...\n";std::cout << "Jesse Liberty is a C++ Programmer!\n";return 0;}

Share on other sites
ItsNotATumaaa,

endl is also part of the 'std' namespace. Make sure to either write "using namespace std;"

at the start of your file. Or add "std::" to the beginning of endl.

ie:
std::cout << "Here is a very big number:\t" << 7000 << std::endl;

Share on other sites
Ahh, thanks, I wasn't sure where the std:: was necessary.

Share on other sites
Quote:
 Original post by ItsNotATumaaaAhh, thanks, I wasn't sure where the std:: was necessary.

std is required for all functions and classes in the standard library, cout, cin, vector, list, deque, map, sort, etc.

Share on other sites
Thanks for all the help, I went ahead and added using namespace std; below my header and removed std:: all together, added a system pause (Because the window disappears after a split second) and it worked.

I think I'm starting to get to the hang of this, (Barely) couldn't do it without all your help though, thanks!

Share on other sites
BTW, you should replace all you "\n" with std::endl.

Regards,

Share on other sites
Quote:
 Original post by Emmanuel DelogetBTW, you should replace all you "\n" with std::endl.Regards,

why? whats wrong with
std::cout <

Share on other sites
hmmm missing most of the last post

std::cout <

Share on other sites
arrrrrrrrrrrrrrrrrrrrr
does the code tag work?

std::cout

and lets try a quote aswell
[quote]
std::cout <

Share on other sites
fuckin stupid board you cant even post code. !!!!!!!!!!!!!!!!!!!!

Share on other sites
AP, I believe there is an issue only with AP's as to what the board allows
you to post.

cout <<
has the <> that would otherwize be taken as HTML braces, and as such are cut from the final AP post.

Share on other sites
The difference between '\n' and std::endl is that std::endl will flush the buffer (std::cout's in this case).

Share on other sites
The problem with using '\n' is that it doesn't flush the buffer, so if the program crashes, there may be text still in the buffer that has not been displayed on the screen. This is misleading because you might assume that the error occurred before the text should have been displayed, when really the error occurred after.

Share on other sites
'endl' is system independent, so basically using endl (end-ell) will add the appropriate line break for the operating system it is running on.

'\n' is not system independent, different operating systems may have a different
line break character.

so using 'endl' is the best and 'safer' option.

Share on other sites
this tag

[ s o u r c e ]
code here
[ s o u r c e ]

(without the spaces)

works fine for me.

Share on other sites
Quote:
 Original post by electronix'endl' is system independent, so basically using endl (end-ell) will add the appropriate line break for the operating system it is running on.'\n' is not system independent, different operating systems may have a differentline break character.so using 'endl' is the best and 'safer' option.

Not true - the c runtime will convert \n into the appropriate endline character - the reason to use std::endl is for the flush

Share on other sites
Quote:
 Original post by MadDog72The problem with using '\n' is that it doesn't flush the buffer, so if the program crashes, there may be text still in the buffer that has not been displayed on the screen. This is misleading because you might assume that the error occurred before the text should have been displayed, when really the error occurred after.

Well... you *should* be outputting "diagnostic" messages to cerr (which is by default not buffered in this way) instead, but your point is otherwise valid. :)

Share on other sites
This will work too. I think too many std:: makes programs harder to read.

#include <iostream>int main(){ using std::cout; using std::endl; cout << "Hello there.\n"; cout << "Here is 5: " << 5 << "\n"; cout << "The manipulator endl writes a new line to the screen."; cout << endl; cout << "Here is a very big number:\t" << 7000 << endl; cout << "Here is the sum of 8 and 5:\t" << 8 + 5 << endl; cout << "Here's a fraction:\t\t" << (float) 5/8 << endl; cout << "And a very very big number:\t"; cout << (double) 7000 * 7000 << endl; cout << "Don't forget to replace Jesse Liberty with your name...\n"; cout << "Jesse Liberty is a C++ Programmer!\n"; return 0;}`

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• Forum Statistics

• Total Topics
627694
• Total Posts
2978672

• 19
• 14
• 12
• 10
• 12