Public Group

# endl error

This topic is 4458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 <

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 27
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001791
×