'endl' and 'end' not recognised

Hi, I've just started a programming book. I've freshly installed Visual Studio Community on Windows 10. I've entered the code for the first 'Hello World' program as shown below, but when I compile it 'endl' and 'end' produce an error stating identifier undefined/undeclared.
Funny thing is if I add 'using namespace std' at beginning, 'endl/end' are recognised but the 'int' on line 'int main()' is not any more!
Furthermore, if I use 'std::endl', it is recognised but the final set of '<<' are not any longer. Have no idea why and of this happens and I cannot compile the code.
Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

int main()
{
	int x = 5;
	int y = 7;
	std::cout << endl;
	std::cout << x + y << " " << x * y;
	std::cout << end;
    return 0;
}
std::end is not what you are looking for: http://www.cplusplus.com/reference/iterator/end/

To use endl as you did, you will have to put std:: in front of it:
 
std::cout << "Hello there!" << std::endl;


The reason why you can't add 'using namespace std' is because you also need a semicolon after that:
 
using namespace std; //<-- I don't do "using namespace", it's a bad habit 
Hello fassintaak,

To expand on what goldenchicken has said. The line using namespace std; or most "using" statements are a bad habit and a bad idea as it WILL get you in trouble some day.

Now is a good time to start learning what is i the standard namespace as most header wrap most of what is there in the standard name space. Your VS can help you with this. Start by typing "std::" and a drop down list box should appear of what is in the standard name space. Then type a letter or two of what you want and that work should become highlighted or at least something close to what you want. You can use the up and down arrow keys to highlight what you want and press tab or enter to finish.

For what you will be using "cout", "cin" and "endl" will all need to be qualified with "std::".

When you miss qualifying something in the standard name space the compiler will let you know that something is wrong. Once you get use to the error messages and and how to correct it it will be easier. Also When VS encounters something wrong in your code it will underline it in red so that you have a chance to fix it before you compile.

If you have any more questions about VS let me know.

Hope that helps,

Andy
Thanks guys very helpful! Back on track... for now ;)
Registered users can post here. Sign in or register to post.