4

I'm new to c programming and I'm facing this problem with my program
I have a loop that gets a char form the input buffer

while(c = getchar()){
    if(c == '\n') break;
    if(c == '1') Add();
    if(c == '2') getInput(); // this is where the headache starts
    ....
}

here is the getInput() function

void getInput()
{ 
    char ch = getchar();
    if(ch == '1') doSomething();
    ....
}

but when calling getchar() from the getInput() function it only gets characters that were left in the input buffer from the last call of getchar(). and what i want it to do is to get newly typed characters.

I've been googling for two hours for a decent way to clear the input buffer but nothing helped. So a link to a tutorial or an article or something is very appreciated and if there's another way to implement this then please tell me.

2 Answers2

1

This should work: (Example of clearing input buffer)

#include <stdio.h> 

int main(void)
{
  int   ch;
  char  buf[BUFSIZ];

  puts("Flushing input");

  while ((ch = getchar()) != '\n' && ch != EOF);

  printf ("Enter some text: ");

  if (fgets(buf, sizeof(buf), stdin))
  {
    printf ("You entered: %s", buf);
  }

  return 0;
}

/*
 * Program output:
 *
 Flushing input
 blah blah blah blah
 Enter some text: hello there
 You entered: hello there
 *
 */
imulsion
  • 8,820
  • 20
  • 54
  • 84
  • thanks for your answer but as you see in the code am trying to get one character that will describe a choice and not a whole string. so what i want is just get the character compare it to another and forget about what's left in the input stream and then call another getchar(). – Abdullah Al-Hatem Dec 05 '12 at 18:34
1

First of all there will be == comparison operator rather than = assignment operator in the if condition in this code.

while(c = getchar()){
    if(c = '\n') break;
    if(c = '1') Add();
    if(c = '2') getInput(); // this is where the headache starts
    ....
}

And for stop taking input try EOF which from keyboard can be given by prssing CTRL+D.

EDIT : The problem is with the \n which is actually taken as input when you press ENTER key on the key board. So change just one line of code.

if (c ==\n) break; to if (c == EOF ) break; and as I said EOF is the end of input.

Then your code will work fine.

Flow of code :

step 1: suppose `2` is input 
step 2: getInput() is called
step 3: suppose `1` as input  // in getInput
step 4: doSomething() is called  // from getInput
step 5: After completion of doSomething again come back to while loop , 

but in your case you have already given `\n` character as an input 

when you pressed `1` and `ENTER`.And thus loop terminates.

but after changing the code as I said , this should work.

NOTE: To understand code flow and for debugging purposes it's best practice to put printf() in various places in functions and see the output as which lines are executing and which are not.

Omkant
  • 9,018
  • 8
  • 39
  • 59
  • i tried it but it doesn't works because between step 1 and step 3 input buffer has to be flushed. but here it is not flushed and the input buffer from the first call of getchar() will affect the comparison in getInput() – Abdullah Al-Hatem Dec 05 '12 at 19:01
  • are you taking input in `Add()` function also , if yes then there may be a problem due to that (You haven't shown us the code of Add) ? Otherwise code worked fine on my system. – Omkant Dec 05 '12 at 19:05
  • I'm not taking input in add()(in fact am just calling printf) and i have changed the code to what you said if you want i can post the code but it's not very well commented and too long – Abdullah Al-Hatem Dec 05 '12 at 19:09