| | |
Reading char from stdin without waiting for input
Thread Solved |
•
•
•
•
Standard C doesn't have anything to do with it.
You need to modify the input stream's mode to "unbuffered". You probably want to turn off "echo" also.
Both of these things are OS-dependent things to do. What OS are you targeting?

I can deal with an extra character printing to the screen in a standard way. I can also handle all the graphics in a standard way. Just need to get the user input without inturrupting the game. I already suggested switching stream mode's in my first post. It look's like the best way to handle it is to have a theird key for "no input".
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Standard C doesn't have any concept of console or graphics displays. Hence, what you are trying to do is technically impossible.
If you are using a graphics library then you should be able to get unbuffered keyboard input from it.
Otherwise, how are you doing the pong game in console? Whatever library you are using should have a way to get unbuffered keyboard input.
Every display and every computer system is different. Libraries like NCurses are written to help mitigate those differences, but they ultimately are OS/hardware bound. There is no such thing as a standard C display.
Turning off buffered-input and echo is really not that difficult to do, nor is polling the keyboard for any ready input.
All we want to know is: system/OS for each target.
Then we can point you in the right direction for the necessarily OS-dependent methods to do what you want.
Holding down a key to keep the program running is, IMO, not a good design decision.
If you are using a graphics library then you should be able to get unbuffered keyboard input from it.
Otherwise, how are you doing the pong game in console? Whatever library you are using should have a way to get unbuffered keyboard input.
Every display and every computer system is different. Libraries like NCurses are written to help mitigate those differences, but they ultimately are OS/hardware bound. There is no such thing as a standard C display.
Turning off buffered-input and echo is really not that difficult to do, nor is polling the keyboard for any ready input.
All we want to know is: system/OS for each target.
Then we can point you in the right direction for the necessarily OS-dependent methods to do what you want.
Holding down a key to keep the program running is, IMO, not a good design decision.
The point is to make it portable, not make a good game. I'm not going to make a game that's been done 1000's of time whithout a twist. The game will compile on any computer that supports C, I am not targeting any platform. Keep the thought of using a 3rd party lib out of this thread!
Assuming that the terminal is no smaller that 80x80 and no bigger than 160 high, graphics are simple without using a library. Use your brain power, you can figure it out. I'll even give you a hint: Figure out a way to erase the screen (can be done in one line of code). Then the rest should be obvious.
>Turning off buffered-input and echo is really not that difficult to do
Read my first post. I already suggested turning off the buffer. Is it portable? (my original question)
Also, turning off echo is not possible in standard C. But it was never a problem. I can deal with it.
I already figured out how to do everything besides get user input. I have already suggested turning the input stream's buffer off. I already asked if it is portable. It seems, even though bad, you can only do it by having a theird input key for "do not move".
Assuming that the terminal is no smaller that 80x80 and no bigger than 160 high, graphics are simple without using a library. Use your brain power, you can figure it out. I'll even give you a hint: Figure out a way to erase the screen (can be done in one line of code). Then the rest should be obvious.
>Turning off buffered-input and echo is really not that difficult to do
Read my first post. I already suggested turning off the buffer. Is it portable? (my original question)
Also, turning off echo is not possible in standard C. But it was never a problem. I can deal with it.
I already figured out how to do everything besides get user input. I have already suggested turning the input stream's buffer off. I already asked if it is portable. It seems, even though bad, you can only do it by having a theird input key for "do not move".
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
•
•
•
•
Assuming that the terminal is no smaller that 80x80 and no bigger than 160 high, graphics are simple without using a library.
•
•
•
•
Q: How can I clear the screen?
A: Such things depend on the terminal type (or display) you're using. You will have to use a library such as termcap, terminfo, or curses, or some system-specific routines, to perform these operations.
•
•
•
•
For clearing the screen, a halfway portable solution is to print a form-feed character ('\f'), which will cause some displays to clear. Even more portable (albeit even more gunky) might be to print enough newlines to scroll everything away (although of course this leaves the cursor at the bottom of the screen, not the top).
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Well, Hiroshe, since your brain power is obviously so much greater than mine, the answer to your input question is simply: you've already figured it out.
Writing portable code has nothing to do with avoiding system dependencies. Rather, it has everything to do with how your code is bound to those dependencies.
But, since you so knowingly disagree, you can continue without me.
Writing portable code has nothing to do with avoiding system dependencies. Rather, it has everything to do with how your code is bound to those dependencies.
But, since you so knowingly disagree, you can continue without me.
•
•
•
•
Well, Hiroshe, since your brain power is obviously so much greater than mine, the answer to your input question is simply: you've already figured it out.
Writing portable code has nothing to do with avoiding system dependencies. Rather, it has everything to do with how your code is bound to those dependencies.
But, since you so knowingly disagree, you can continue without me.
As long as the code can run on a platform with: a keybored, 80x80 ascii terminal and support for C, It would be (for this thread)"portable."
I know what you mean by "how your code is bound to those dependencies," but as I said, that was not the goal. If I was making the game for real, I would follow your advice (good advice which I agree with), however I want to do something different (just for the sake of doing it). I didn't mean to say your advice was not helpful, but it's out of scope.
Thanks for helping.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
![]() |
Similar Threads
- how how to write a C source code for a Login system? (C)
- How do I convert telephone phrases to numbers? (C++)
- Not waiting for input during loop (C++)
- My own read function (C)
- Need Help in Reading characters from a text file (C++)
- C program (not C++) in/out putting a entire sentence (C++)
- system("PAUSE") (C++)
- Help with input (C)
- reading a file into code (Java)
Other Threads in the C Forum
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char character cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






