Question:
C programming concerning char input?
kiara_kanzaki
2008-07-31 21:03:26 UTC
I've made a program that has a lot of getch() commands and asking for a lot of char inputs from the user (at least five). Problem is, whenever it would ask for a char input, and i press any letter/key it would automatically move on to another question, which in this case is asking for an int input (This one has no problems and always works). Sometimes it would just skip the char input question, which would render my program useless at this point.

I've tried debugging it, no problems there. What I'm trying to think of is how the program would actually wait and register the char input from the user. It's been bugging me that this is the only untraceable bug I have and haven't resolved over two weeks. Any help please? I can't post the code though. Just a tip or an explanation would be appreciated.
Three answers:
Akira Matsushima
2008-07-31 21:25:32 UTC
When I did my programming in C, I also encountered a similar situation. What I found is that you cannot request a char input more than two (at least I think) successively. What I do is prompt for the input and avoid using successive char request. The same goes if you want to get a String using gets(). Sometimes a loop to get the input will also cause this problem

e.g:

aChar1 = scanf();

aChar2 = scanf();// don't do this



aChar1 = scanf();

printf("");// you can prompt for input if you want to

aChar2 = scanf();



Hope I help a little.
iqbal
2008-07-31 21:55:13 UTC
Hi;

before reading a charachter through getch();, u could use fflush(stdin); statement. It will clear (Flush) Standard Input (Keyboard). Sometimes some characters remain in the keyboard buffer. They may also cause this problem.
durgin
2016-10-22 10:24:02 UTC
the basic e book -- which covers the 1986 C usual -- is the c language with the help of Kernighan and Ritchie. at the same time as thorough it really is way less complicated than the language itself. It basically repays sluggish and careful interpreting handsomely. various applications in the conventional library are shown with code to finish them as an instance.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...