Differences between std::cin.ignore() and std::cin.rdbuf()->in_avail()+1

This is a discussion on Differences between std::cin.ignore() and std::cin.rdbuf()->in_avail()+1 within the C++ Programming forums, part of the General Programming Boards category; can someone tell me the difference between Code: std::cin.ignore() and Code: std::cin.rdbuf()->in_avail()+1 I was reading a book and it told ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    17

    Differences between std::cin.ignore() and std::cin.rdbuf()->in_avail()+1

    can someone tell me the difference between
    Code:
    std::cin.ignore()
    and
    Code:
    std::cin.rdbuf()->in_avail()+1
    I was reading a book and it told me to input
    Code:
    std::cin.rdbuf()->in_avail()+1
    I figured out that putting
    Code:
    cin.ignore()
    would produce the same result, by letting the program to wait for user to press enter before ending the program.

    Also, can someone tell me how does
    Code:
    std::cin.rdbuf()->in_avail()+1
    work?

    Thanks in advance ^^ Merry Chrismas~

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    It sounds really dodgy to me. I might be wrong though.

    So it asks for the read buffer, gets the number of characters still in the buffer (which, I think, is implementation dependent so means next to nothing for your program). It returns an integer (streamsize type to be exact), which is not a reference. So adding one to it will do absolutely nothing unless you use the result.

    So, it sounds to me like it should do nothing at all. Or is this some kind of stupid Microsoft stuff?

    Are you sure that a character is ignored with just that line?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    std::cin.rdbuf()->in_avail()+1
    This value is one more than the number of characters that the C++ runtime has processed and is ready to be "used" - it is not necessarily [in fact, most likely NOT] the number of characters that the user has entered, that are available to read. The only thing you know for certain is that there is AT LEAST that much in the input buffer - so cin.read() or something similar will not block until AT LEAST this number has been read in.

    You would definitely need to combine this with cin.ignore() or some other "consuming" function.

    Edit: Link to better explanation: http://www.tech-archive.net/Archive/.../msg00389.html

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21