|
-
Nov 20th, 2002, 06:37 AM
#1
Thread Starter
Lively Member
IOSTREAM problem
Hi!
I am basically completely new to VC++ and I wonder, when am I to include this:
#include <iostream>
Thank you for replying!
VVT
-
Nov 20th, 2002, 06:51 AM
#2
Stuck in the 80s
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.
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;
}
Basic C++ program using iostream.
-
Nov 20th, 2002, 08:54 AM
#3
Thread Starter
Lively Member
Thanks for the help. Its much appreciated.
-
Nov 20th, 2002, 01:56 PM
#4
Stuck in the 80s
Just incase you're wondering, this is how you'd have to do it without the "using namespace std;" line:
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;
}
For some strange reason, I prefer the first example.
-
Nov 22nd, 2002, 01:41 AM
#5
Thread Starter
Lively Member
Thanks again.
I think I also will use the first example.
-
Nov 22nd, 2002, 07:14 AM
#6
Fanatic Member
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:
Not for me, it works fine all the time, unless that namespace is used by vc++ 6 automatically.
-
Nov 22nd, 2002, 07:31 AM
#7
Monday Morning Lunatic
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:
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;
}
For some strange reason, I prefer the first example.
Actually, you can get away with:
Code:
std::cout << "Hey!" << endl;
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.
Don't think most compilers can do it yet, despite it being about 2 years since it was added to the standard...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 22nd, 2002, 09:03 AM
#8
Stuck in the 80s
Originally posted by parksie
Actually, you can get away with:
Code:
std::cout << "Hey!" << endl;
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.
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?
-
Nov 22nd, 2002, 09:14 AM
#9
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 22nd, 2002, 09:17 AM
#10
Monday Morning Lunatic
It's by expression.
Consider that:
Code:
std::cout << "hello: " << 50 << endl;
resolves to:
Code:
endl((std::cout).operator<<("hello: ").operator<<(50));
This is because of the definition for GCC's endl (inside std::):
Code:
template<typename _CharT, typename _Traits>
basic_ostream<_CharT, _Traits>&
endl(basic_ostream<_CharT, _Traits>& __os)
{ return flush(__os.put(__os.widen('\n'))); }
As you can see, it cannot resolve the endl, so it's allowed to look in the namespace containing the (resolved) std::cout.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 23rd, 2002, 01:46 PM
#11
Fanatic Member
Originally posted by CornedBee
riis: are you sure you include <iostream> and not <iostream.h>?
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?
-
Nov 23rd, 2002, 01:49 PM
#12
Stuck in the 80s
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.h> is the older, deprecated version, still included for backwards compatability (correct me if I'm wrong).
<iostream> is part of the STL (again, correct me if I'm wrong).
-
Nov 23rd, 2002, 03:14 PM
#13
Monday Morning Lunatic
<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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|