Instant cin?

I always use "cin >> *" with switch(*), but it need to press enter after i write the variable's value, but if it's only 1 character long i would like to use an "instant" cin, for example i press 4 and i instantly get X message for case '4' without needing to press enter before.
Last edited on
Doing stuff like this relies on examining the keyboard buffer and that is OS dependent. There is a kbhit()/getch() combo that does what you want but it is not strictly "standard."
So i write kbhit(); getch(); then i have "instant cin"?
IS there another way of doing this without conio.h?

I can't find a way for it to work as cin >> *; switch(*);
Last edited on
No you can't use streams like that. The user would have to hit enter for the stream to process the input.
Sounds like your only way to go is either with something non portable, non standard, or using a third class library. C++ doesn't handle hardware, in this case keyboard, since it doesn't know it exists. That is why there is all of these added features in OS dependent libraries, non standard libraries, like conio.h, and third party libraries.
C++ doesn't handle hardware


I would say that is a gross overstatement.
Honestly, I wouldn't say it's a gross overstatement. The only thing I missed out on was saying Standard C++ doesn't handle hardware, because quite frankly it doesn't. The OS handles it. Non standard libraries and third party libraries handle hardware because they have found ways to do it. If you know an exception to this, by all means, please tell me about it.
instead of cin use getch() with the library conio.h

so instead write cin>>x;
write x=getch();

and will be instant cin.:D
getch() is not standard. In fact, nothing in conio.h is standard and it's bad to learn non standard libraries since they can vary on how they act between different systems.
Volatile Pulse wrote:
it's bad to learn non standard libraries

There are many things you can't do with only standard C++. Using non-standard libraries are often necessary, which is the case here.
Last edited on
closed account (o3hC5Di1)
Volatile Pulse wrote:
The only thing I missed out on was saying Standard C++ doesn't handle hardware, because quite frankly it doesn't. The OS handles it.


Aren't there OS's / drivers out there written in C++ ?
How would those be written if c++ doesn't handle hardware?
Is that perhaps what Mr. ResidentBiscuit meant - that it doesn't handle it automagically, but it can?

Just asking, out of interest.

All the best,
NwN
This sounds as if you are trying to make a game, like with the WASD keys, and if this is the case, then you should get a game library. One I like is Allegro, you can get it here: http://alleg.sourceforge.net/download.html
hope this helps!
@NwN
I didn't say C++ wasn't capable of handling hardware. It's just not something that's easy to implement like I believe the OP wants. I know some of the power of C++, and yes, a lot of things are written in C++, but is it pure C++? Doesn't it typically require a lot of external stuff to make it work? I don't know of anything in the standard libraries that would allow anyone to handle hardware without using, at the bare minimum, either an OS dependent or third party library. Maybe I'm lacking some serious understanding in the hardware portion of C++, but I've been told "that can't be done" when it comes to hardware and standard C++.

I never mention that something can't be done, because quite frankly, I'm not sure of what can't be done with C++.
Topic archived. No new replies allowed.