PDA

View Full Version : why should I use std::cout (namespace)



brijith
April 17th, 2008, 01:02 PM
hai,

In my college days I used to write CPP programs as below


#include <iostream.h>

int main()
{
char s[10];
cout << "Hello World!" << std::endl;
cin >> s;
cout<< s<<"\n";
return 0;
}

that day we were using ReadHat 9. When I tried the same code in new Linux distribution it is not working

for the above code to work I have to change it as below


#include <iostream>

int main()
{
char s[10];
std::cout << "Hello World!" << std::endl;
std::cin >> s;
std::cout<< s<<"\n";
return 0;
}




Why this change? can any body suggest a tutorial where I can get a simple note about these type of changes.

I am refreshing C++ after a long period of time.


Thanks

pedro_orange
April 17th, 2008, 01:44 PM
You can still use cout (without the std:: )

You just need to define

using std::cout;
or
using namespace std;

It's just including namespaces so you can use their functions.

EDIT: Also, why would you use std::endl and not std::cout?
EDIT2: Blonde moment

Caduceus
April 17th, 2008, 01:52 PM
Another way is:

#include <iostream>
using namespace std;

[code]

brijith
April 17th, 2008, 02:34 PM
Thanks friends. May I know more about namespaces

Zugzwang
April 17th, 2008, 02:47 PM
Thanks friends. May I know more about namespaces

Sure. Read chapter 10.2 of Bruce Eckel's "Thinking in C++" (first volume). The book is available free of charge as PDF - use google to find it.

pedro_orange
April 17th, 2008, 02:48 PM
Yes you may sir!

http://www.cplusplus.com/doc/tutorial/namespaces.html