Advertisement

iostream.h / iostream

Started by January 03, 2003 06:48 PM
8 comments, last by deal 21 years, 10 months ago
can someone explain the difference and tell me which of these should i be using ? #include <iostream.h> cout << " "; etc. //------------------ #include <iostream> using namespace sdt; // or using sdt::cout etc. cout << " "; // or sdt::cout etc. very confusing, i have been using #include <iostream.h> because i was using an older book, and thats what they were using, i got given a newer book and it uses #include <iostream> im tempted to continue to use iostream.h someone tell me why i shouldnt do this ?
Use #include <iostream>

[edited by - neurokaotix on January 3, 2003 7:51:52 PM]
Advertisement
#include<iostream>

is correct.

#include<iostream.h>

is not.
#include <iostream> // The way of the future
using namespace std; // ditto

#include <iostream.h> // In case the future has not yet arrived

adding the .h is the old C++ style, and may be used if you are using an old compiler, or else, you should stick to #include <iostream>

[Edit]: Noticed u got more.. ok

cout is a output message code that can write whatever that is in the "". the << is for output, and >> is for input (text you write). If you do not use the ; at the end, you will get an error, because the compiler thinks that the whole cout << "" is a header, and it's not.

Strip it down, and the code will be:

int main() - Header
{ - start function
statement ( cout << ""; )
return 0; - because it's just so that u must use it in int main()
} - terminate function

It's a bit hard to understand in the beginning, but u will understand it later

std::cout and so is special.. instead of using a long list of std's (cout, cin etc.), u can be lasy, and write :
using namespace std;

so the list is shortened.
But the problem is, if you have an old compiler, it might not regognise the new c++ style with: using namespace std;
and that's where you have to use std::cout

and // is comments.. you can also use /* comment */

Hope that helps! and don't lose hope.. Can you believe I am a newbie with the book "C++ Primer Plus Fourth Edition" by Stephen Prata, And I just started yesterday? :D



[edited by - wolferey on January 3, 2003 8:16:07 PM]
but why?
The old-style headers such as were written prior to the C++ standardisation. They are not part of the C++ standard. is part of the standard C++ library. Therefore, the former is not guaranteed to be supplied with a C++ compiler (if it is, this is only for the sake of legacy compatibility), while the latter is. In short: The former is old, deprecated, and may or may not work with any given compiler; the latter is up-to-date and part of the standard. It also makes use of namespaces.
Advertisement
hehe wolferey i was just wondering what was the difference between iostream.h and iostream,

im currently trying to understand virtual copy constructors, got bored so took a break (btw i love reading c vs c++ threads they really get people going dont they ), iostream/iostream.h had been bothering me, i knew how to use both, my compiler works with both, while i was here i thought i would find out which i should be using.

got the answer now so thanx.
Np.. That''s what the forums are for.. to learn and get help.. right? right people..? Stop looking so wierd at me.. *runs and hides*
something that I never understood was I started off with in the current program I''m working on and got it to compile fine. As soon as I changed it to I got 14 compile errors! Why is that.

-----------------------------
Programming is confussing!
AIM: Trebor DoD
-----------------------------AIM: Trebor DoDHompage: Thinking Digitally: My Web Blog
quote: Original post by Digigamer15
something that I never understood was I started off with in the current program I''m working on and got it to compile fine. As soon as I changed it to I got 14 compile errors! Why is that.


Among other things, as with (theoretically) all other standard C++ headers, <iostream> puts its declarations in the std namespace. Either prefix every use of symbols it declares with std:: (e.g. std::cout) to fully scope them, or add a using namespace std; declaration in your .cpp file. It is strongly advised to always use fully scoped declarations in header files (i.e. don''t put a using namespace std; in a .h file), since doing otherwise defeats the purpose of namespaces.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement