Results 1 to 13 of 13

Thread: IOSTREAM problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Sweden
    Posts
    84

    Exclamation 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
    / VVT

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Sweden
    Posts
    84
    Thanks for the help. Its much appreciated.
    / VVT

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Sweden
    Posts
    84
    Thanks again.

    I think I also will use the first example.
    / VVT

  6. #6
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    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.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  11. #11
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    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?

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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).
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    <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
  •  



Click Here to Expand Forum to Full Width