Using kbhit in C and C++

While programming in C/C++, many a times we need to do something till the user presses a key and then decide on the further course of action in the program based upon the key input. Such a logic or program flow can be implemented using a very simple function “kbhit()”.

We are going to explain the use of the same here in following few lines.

The funciton prototype is
int kbhit();

Thus, as we see the function accepts no argument and returns an integer value. This return type remains ‘0’ (zero) till the user does not presses a key and becomes non-zero as soon as the user presses a key.

Now, if we also want to know which key the user has pressed, we have to use the getch() function alongwith kbhit() function.

A very simple code snippet showing the use of both is shown below:-


while (!kbhit())
{
......
//do cool processing here in this loop, which has to continue happening till user presses a key
.....
}
//Now the user has pressed a key
//To know which key we do following
int ch; //it can be char ch; also as u like it
ch=getch();
//now we have the user input in the variable ch
//we can decide further course of action based upon value in ch
//like as follows
if (ch==89)
{
....
//we do something if user pressed 'Y'
// note that 89 is ASCII code for 'Y'
....
}
else
{
....
//user did not press 'Y'
//so we do something else
.....
}
....

We have shown use of if statement here, while we can use any other conditional statements like while or do..while, case etc with ch depending upon our needs.

Keep on doing cool programming, as we say programming is an “Art of playing with logic”…..

Internet Connection Ok But No Content Use Public DNS

Many a times we run into a very unique problem wherein we see that though we are connected properly to our internet service provider (ISP), but we are able to access a few sites only while others may not load or return a “host not found at given address” error.

Typical symptoms of above problem will be”

1. The connection to the ISP is shown as connected on your PC.
2. If you are connected through ADSL modem, you see the DSL light glowing constantly as well as the internet LED blinking, showing internet activity OK.
3. You are able to see the web page of few sites, while others may not load at all.
4. Your internet access time is very slow for certain sites.
5. Till very recently you had a perfect internet connection and suddenly you are not able to access certain sites etc.

Above may be a result of improper DNS i.e. Domain Name Server settings in your network settings.
Generally we are provided with a DNS server setting which our PC fetches from the ISP at connection start up. God forbid if the ISP’s configured DNS server goes down, then you run into the above kind of problems.

Thats too much for the explaination of the problem, but whats the solution????

All you have to do is to set up your DNS entry to a public DNS may be one hosted by Google (at 8.8.8.8) and this is how you do it:

On Windows platform:

1. Open Network Settings in Control Panel.
2. Right Click and select properties on the network adapter, through which you connect to your ISP i.e. through which you access your internet connection. It can be the wireless adapter if you connect through wi-fi or LAN/ ethernet card if you connect through UTP cable.
3. A pop up window will show you certain properties of your network adapter. Scroll down till you see an entry “TCP/IP”. Select it and click on properties button below (which gets highlighted as you select “TCP/IP”)
4. Another pop-up window will show you the IP address and DNS settings.
5. Go down to the Domain Name Server (DNS) address. Generally it will be selected as “Obtain automatically from ISP”.
6. Change it to “Use following DNS server” and punch the following values into the address field:

8.8.8.8

7. Click on OK buttons till you reach back to the network connections window.
8. Now you may check for loading of websites in your browser, and the problem should be resolved.

Happy Surfing…..

Prevent Spread of Virus Through USB Devices

To prevent spread of virus through USB devices (memory sticks, memory cards, other plug n play devices with their own memory/ storage, follow a very simple trick.

When you plug in the device and Windows ask you what to do, do nothing and click cancel.

To browse for the files on the device, never (R) never double click and open the folder icon. Instead, right click and select the explore option. preetisblog.com

This will prevent majority of the Virus programs from running, which are configured to run in place of the auto-run programs/ hidden in the open command of Windows.

Wish u a safe n happy computer usage……