Instant cin?

Jul 20, 2012 at 4:18pm
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 Jul 20, 2012 at 4:19pm
Jul 20, 2012 at 4:47pm
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."
Jul 20, 2012 at 5:31pm
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 Jul 20, 2012 at 5:41pm
Jul 20, 2012 at 6:04pm
No you can't use streams like that. The user would have to hit enter for the stream to process the input.
Jul 20, 2012 at 6:10pm
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.
Jul 20, 2012 at 6:21pm
C++ doesn't handle hardware


I would say that is a gross overstatement.
Jul 20, 2012 at 7:37pm
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.
Jul 20, 2012 at 7:50pm
instead of cin use getch() with the library conio.h

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

and will be instant cin.:D
Jul 21, 2012 at 4:51am
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.
Jul 21, 2012 at 8:12am
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 Jul 21, 2012 at 8:12am
Jul 21, 2012 at 8:59am
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
Jul 21, 2012 at 1:16pm
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!
Jul 21, 2012 at 2:01pm
@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.