+ 1
confusion!
hi,i am a new programmer....i am begining to learn c++ and i made a my first calculator(kinda): #include <iostream> int main(){ int a,b,result; std::cout<<"enter first number:"; std::cin>>a; std::cout<<"\n"; std::cout<<"enter second number:"; std::cin>>b; std::cout<<"\n"; result=a*b; std::cout<<"the result is:"<<result; return 0;} and it works too...but the confusion is when i used "std::cout<<endl:" instead of "std::cout"\n" it didnt work, why is that? thank you for your support!
9 Answers
+ 4
Not slower but it is bad practice. If you do "using namespace std;" at the beginning of your program, you can't ever have a variable callend endl or cout or lots of others - because all of std gets dumped into your namespace. Since std is so big, nameclashes like that are a real problem!
If you are going to use it, do it inside functions. For example
int main(){
using namespace std;
cout << ...
}
That way You don't have to write "std::" in your main function, but you don't run into problems everywhere else. The way you did it is totally fine!
+ 5
it's std::cout << std::endl;
+ 5
oh, thank you very much.
+ 2
Just write
using namespace std;
at the beginning of the program so you dont have to write std:: every time you use a function of the standard library.
Your method is fine too
Just a suggestion
+ 1
yea, I know that but someone told me if you use that at the beginning ,your program runs slower than it should be...Is it true? ,if somebody knows please tell me..
0
oh..and one more thing, for example this is a program..
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
cout << "I love programming!";
return 0;
}
now...how do you write the same exact program without using 'using namespace std;'
0
Just put std:: in front of cout and endl like you did in your original post.