Hi!
I am basically completely new to VC++ and I wonder, when am I to include this:
#include <iostream>
Thank you for replying!
VVT
Printable View
Hi!
I am basically completely new to VC++ and I wonder, when am I to include this:
#include <iostream>
Thank you for replying!
VVT
Anytime you want to use "cout << " or "cin >>" to sent output or get input. Make sure you also have "using namespace std;" under that, otherwise you have to do some weird things to get your code to work.
Basic C++ program using iostream.Code:#include <iostream>
using namespace std;
int main() {
int num_cats = 0;
cout << "How many cats?: ";
cin >> num_cats;
cin << endl << "You have " << num_cats << " cats!? << endl;
return 0;
}
Thanks for the help. Its much appreciated. :)
Just incase you're wondering, this is how you'd have to do it without the "using namespace std;" line:
For some strange reason, I prefer the first example. ;)Code:#include <iostream>
int main() {
int num_cats = 0;
std::cout << "How many cats?: ";
std::cin >> num_cats;
std::cout << std::endl << "You have " << num_cats << " cats!";
std::cout << std::endl;
return 0;
}
Thanks again.
I think I also will use the first example.
Not for me, it works fine all the time, unless that namespace is used by vc++ 6 automatically.Quote:
Originally posted by The Hobo
Just incase you're wondering, this is how you'd have to do it without the "using namespace std;" line:
Actually, you can get away with:Quote:
Originally posted by The Hobo
Just incase you're wondering, this is how you'd have to do it without the "using namespace std;" line:
For some strange reason, I prefer the first example. ;)Code:#include <iostream>
int main() {
int num_cats = 0;
std::cout << "How many cats?: ";
std::cin >> num_cats;
std::cout << std::endl << "You have " << num_cats << " cats!";
std::cout << std::endl;
return 0;
}
This is called Koenig Lookup (after a C++ expert that proposed it, I think). It basically means that if the compiler cannot resolve a name in the namespace it's in at the moment, it's permitted to look in the namespace containing the other arguments, in this case, that of cout (std), and it finds endl in there, and it works.Code:std::cout << "Hey!" << endl;
Don't think most compilers can do it yet, despite it being about 2 years since it was added to the standard...
Is that basically by line, then? Or does it look in the last std provided?Quote:
Originally posted by parksie
Actually, you can get away with:This is called Koenig Lookup (after a C++ expert that proposed it, I think). It basically means that if the compiler cannot resolve a name in the namespace it's in at the moment, it's permitted to look in the namespace containing the other arguments, in this case, that of cout (std), and it finds endl in there, and it works.Code:std::cout << "Hey!" << endl;
Don't think most compilers can do it yet, despite it being about 2 years since it was added to the standard...
Never heard of that. But since not many compilers support it I wouldn't use it. I don't think it increases readability.
riis: are you sure you include <iostream> and not <iostream.h>? Because I know for SURE that VC++6 wouldn't accept using elements from <iostream> without somehow referencing the std namespace.
It's by expression.
Consider that:resolves to:Code:std::cout << "hello: " << 50 << endl;
This is because of the definition for GCC's endl (inside std::):Code:endl((std::cout).operator<<("hello: ").operator<<(50));
As you can see, it cannot resolve the endl, so it's allowed to look in the namespace containing the (resolved) std::cout.Code:template<typename _CharT, typename _Traits>
basic_ostream<_CharT, _Traits>&
endl(basic_ostream<_CharT, _Traits>& __os)
{ return flush(__os.put(__os.widen('\n'))); }
Oops, I didn't see the missing .h . I'm always using <iostream.h> instead of <iostream>. I didn't know there was a difference. What's actually the reason for this difference?Quote:
Originally posted by CornedBee
riis: are you sure you include <iostream> and not <iostream.h>?
<iostream.h> is the older, deprecated version, still included for backwards compatability (correct me if I'm wrong).Quote:
Originally posted by riis
Oops, I didn't see the missing .h . I'm always using <iostream.h> instead of <iostream>. I didn't know there was a difference. What's actually the reason for this difference?
<iostream> is part of the STL (again, correct me if I'm wrong).
<iostream> is part of the Standard C++ Library, of which the STL is a major component. The STL itself is just the containers and algorithms.