Wolfpack'08
Wolfpack'08

Reputation: 4128

Game programming in c++, what's this old line, and why doesn't it work?

Here's a line of code that I get an error about, which is in my book from 2008: std::cin.get(std::cin.rdbuff()->in_avail()+1);

Could someone please tell me what it means and why it gives me an error:

In function 'int main()':|7|error: 'struct std::istream' has no member named 'rdbuff'|
||=== Build finished: 1 errors, 0 warnings ===|

--update--

changed rdbuff to rdbuf, and it throws this error:

walkthrough.cpp|7|error: no matching function for call to 'std::basic_istream<char, std::char_traits<char> >::get(std::streamsize)'|

Upvotes: 0

Views: 270

Answers (2)

celtschk
celtschk

Reputation: 19731

Kerrek SB already told you the reason for the error: It's rdbuf, not rdbuff.

Now on what this code tries to do: It is trying to enforce a blocking read (that is, it tries to force the program to wait for the user to input something even if there's unread stuff which has been entered before reaching that statement). in_avail gives the number of characters "available", that is, how many characters you are guaranteed to be able to read without blocking, i.e. without the program having to wait for the user to input more.

However that line of code is somewhat misguided because there's no guarantee that reading the next character does block. So it may happen on some system that this line still doesn't wait for user input in some cases.

And even in cases where it does, the input stream will typically be left with a line where the first character was removed, but the rest is available, possibly giving surprising results on further reads if it wasn't an empty line to begin with.

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 477338

The member function is called rdbuf(), one f, as a cursory glance at any library reference will easily reveal.

In other words, the error is that istream has no member named rdbuff.

Upvotes: 4

Related Questions