Jump to content
  • Advertisement
Sign in to follow this  
ItsNotATumaaa

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by ItsNotATumaaa
Ahh, 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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Emmanuel Deloget
BTW, you should replace all you "\n" with std::endl.

Regards,


why? whats wrong with
std::cout <

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hmmm missing most of the last post

std::cout <

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!