Is there ANY standards compliant way to get unbuffered input from the console without the characters being automatically displayed?

because, although I can reset the buffer on cin to 0 with cin.rdbuf->pubsetbuf(0,0) , when I do this, it outputs all characters twice (because it outputs them by default once?)

The reason this is necessary, is I am writing a program to do mathematical operations on numbers larger than those available in standard C++ datatypes, so I want to read 2 numbers from the console. I want to implement this in a way that the user can ONLY enter digits into the console, because it is cleaner than allowing an input, then erroring and asking for a correct input.

I've searched, and seen this question raised in other places, with the OP eventually either using getch(), or getting convinced that they really don't need the same functionality.


So, does anyone know of a standards compliant way to get the same functionality of getch() (and putch(), for that matter)?

Recommended Answers

All 3 Replies

There is no standard way to read characters like getch()

Is there ANY standards compliant way to get unbuffered input from the console without the characters being automatically displayed?

Short answer, no. The console does its own buffering, and then C++ does another level of buffering for performance. You can change C++'s buffering, but that only affects performance. By the time your program gets the characters, they have already been displayed on the console.

The only way to change the console's buffering is with a system level API, and that is not portable. That is what getch() does, but the standard does not define a function with getch()'s behavior.

Ok, thanks for the responses!

So, my only option is to either use getch() and deal with the fact that it isn't in the standard, or to use cin and do it the less "pretty" way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.