[C Programming] Clear input buffer

When programming, sometimes you will need to clear the input buffer to make sure there won’t be anything miscellaneous inside it. The most popular problem (I think) related to input buffer and input data is: fgets() function will not pause to let you enter data when it detects an ‘\n’ character inside the buffer (or stdin). But why is there a ‘\n’ char? Mostly because you have used scanf() (or some functions similar to it) right so it left ‘\n’ char.

And what is the solution? Well, we’ll create a function clear_buffer() to do it.

int clear_buffer() {
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
return 0;
}

(assumed you have included stdio.h in the main program for getchar() to work)
Now you can call this function everywhere you really need to flush the buffer. I said “really” because if there’s nothing in the buffer, the program will wait until there is ‘\n’ (i.e you have to press enter).

Credits to CProgramming.com Forum

[Eclipse] SWT browser is not available

Phew! Then, this is my first post using this blogging system (I have been using WordPress self-hosted to build my website but haven’t used this WordPress.com to blog yet). Cheer!

Now, get to the point!

I’m using Eclipse PDT to learn PHP. In the past, I used ubuntu 10.10 and didn’t have a problem using the SWT browser integrated inside Eclipse. But, after changing to Natty (11.04), I realized this problem: “SWT browser is not available” when I want to view the webpage right inside Eclipse. Well, after googling for sometime, I found that it need at least 3 things to run:

  • Mozilla 1.4
  • GTK 2.*
  • XULRunner 1.7.x – 1.9.x

The first two conditions are satisfied out-of-the-box. But when I used Synaptic to search, in Natty, the version of XULRunner is 2.x, not 1.9.x. Fortunately, there also exists the package xulrunner-1.9.2 for you install. Just install it (then restart eclipse if it’s openning) and, voila => It’s surely available now, right?