Question:

C programming concerning char input?

by  |  earlier

0 LIKES UnLike

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.

 Tags:

   Report

2 ANSWERS


  1. 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.


  2. 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.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.