Hello semsemdiver,
Back when I first started here "using namespace std:" was frowned upon and many people were saying not to use it. Also putting things like "#include <iostrean>" and others in a header is wrong.
At some time this appears to have changed and I missed it. It seems now that putting header files in a header file that you write is OK if needed. And that using the line "using namespace std;" is being used by some of the same people that use to say not to.
Based on what I was reading, and even though I did not fully understand it back then, I started qualifying everything with "std::" and started to learn what was in the standard name space.
If you are not familiar with name spaces these should help.
http://www.cplusplus.com/doc/tutorial/namespaces/
https://www.learncpp.com/cpp-tutorial/4-3b-namespaces/
After about a week and a half I used it without thinking about it. The link provided should help you to understand. If not try a search. I think "using namespace std" should work. There are many other posts on this topic.
I have also come to accept that some people pay a lot of money to take a class and to be taught the wrong way to code and think it is the only way to code.
Using namespace standard in the global scope of the file
WILL get you in trouble some day.
In your early days in school you were taught that 1 + 1 = 2. Do you need examples to prove this every time you see it or do you just accept that it is a fact.
When it comes to using "std::" I accept this as a fact and realized that it will become more clear in the future.
When you start programming what you are likely to use first is "std::cin", "std::cout", "std::endl" and "std::string" with "std::vector" maybe coming next. As you include more header files there is more that is put in the standard namespace and more that you will need to learn how to qualify.
The question comes do you want to learn a little bit at a time or everything all at once?
There is also the possibility that when you are hired by a software company you could be told the "using namespace std;" will never be allowed in a program. Now do you want to learn everything that is in the standard namespace at one time? Just saying.
You said:
the code has error when i built it ?!! |
What error? Where? Be more specific and include the error message as the compiler gave it to you.
In VS 2017 the program compiles with no errors or warnings, so I have no idea what your problem is. Also I do not believe I know what IDE/ compiler you are using and on what operating system. This helps.
Hope that helps,
Andy