Thread: Istream cin vs. cin.get

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    5

    Istream cin vs. cin.get

    Hi
    Code:
        char str1[30];
        char str2[30] ;
        cin >> str1 ;
        //cout.flush();
        cin.get(str2,30);
    Why the cin.get() doesn't work ?
    Thanks .

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Use this:
    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
     
    int main()
    {
        char str1[30];
        char str2[30] ;
        cin >> str1 ;
        //cout.flush();
        cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
        cin.get(str2,30);
        system("Pause");
    }
    After cin>> str1, newline character stayed in the input stream.
    That's why you can use ignore member function!
    Last edited by Micko; 08-30-2006 at 11:05 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
    To be strictly correct you should include <ios> for streamsize.
    My best code is written with the delete key.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Prelude
    >cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
    To be strictly correct you should include <ios> for streamsize.
    Does that mean this code won't compile on some other compilers because of this?
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    I'll appreciated if a little explain numeric_limits<>::max()
    anyway , it was helpful ,thx
    Last edited by Mehdi; 08-31-2006 at 04:08 AM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Does that mean this code won't compile on some other compilers because of this?
    It means that the standard doesn't require it to. However, I have yet to see an iostream header that doesn't include ios in some way, shape, or form. But that too isn't required, it just happens to be the case on every compiler I've seen.

    >I'll appreciated if a little explain numeric_limits<>::max()
    numeric_limits is a template class that's specialized for different types. There are several properties of each type, but let's just say that there's only max and min for the sake of my carpal tunnel. numeric limits would be defined like this:
    Code:
    namespace std {
      template <typename T>
      class numeric_limits {
        static T min();
        static T max();
      };
    }
    Then it would be specialized for each type supported. Let's say int and char:
    Code:
    #include <climits>
    
    namespace std {
      template <>
      class numeric_limits<int> {
        static T min() { return INT_MIN; }
        static T max() { return INT_MAX; }
      };
    
      template <>
      class numeric_limits<char> {
        static T min() { return CHAR_MIN; }
        static T max() { return CHAR_MAX; }
      };
    }
    Repeat for all other types you want to support and you have a convenient and consistent interface for the arithmetic properties of various types.
    My best code is written with the delete key.

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I think that
    Code:
    numeric_limits<streamsize>::max()
    is same as
    Code:
    numeric_limits<int>::max()
    At least, it's on every compiler I checked...
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    It may be so, but...

    1. Makes little sense to check for int when we want to check for stream.

    2. Did you check enough compilers and enough operating systems? What if one fails?

    Many other examples there are where one could possibly check against names other than those specified by the implementation. For instance, size_type is known to be an unsigned integral type. So, it's possible to believe that checking against a unsigned long will be enough to know how many elements a container can hold. Or is it an int? Or is it something else specified by the compiler?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I think that
    >numeric_limits<streamsize>::max()
    >is same as
    >numeric_limits<int>::max()
    It may be. Then again, it may not be. Typedef's are there are a reason, and you should take advantage of them if you want portable code.
    My best code is written with the delete key.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Have you checked on GCC for 64-bit systems?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by CornedBee
    Have you checked on GCC for 64-bit systems?
    No I didn't check on any 64-bit system.
    From what I have read here and in other threads I can conclude that there will be a lot of problems when porting existing C/C++ code on 64 bit systems. I'm not C/C++ programmer, but it seems to me that many of them are not really addicted to the standard.

    Thank you for your explanations
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Micko
    From what I have read here and in other threads I can conclude that there will be a lot of problems when porting existing C/C++ code on 64 bit systems.
    Only poorly written code. Code that relies on the size of a built-in data type, when no C or C++ standard ever made any guarantees about it, or about data types being the same size.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. istream
    By Beowolf in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2007, 05:11 PM
  2. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. ostream and istream
    By xddxogm3 in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2003, 07:36 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM