Saturday, November 9, 2013

Be cautious about getch()

        Always press Enter key to come back to your editor window after viewing the program output. Why?
Because,
If you press Up Arrow key to come out from output window to editor window, and if you run the program which takes an input from the keyboard, then the H character appears next to the input prompt.
If you press Right Arrow key to come out from output window to editor window, and if you run the program which takes an input from the keyboard, then the M character appears next to the input prompt.
If you press Down Arrow key to come out from output window to editor window, and if you run the program which takes an input from the keyboard, then the P character appears next to the input prompt.
If you press Left Arrow key to come out from output window to editor window, and if you run the program which takes an input from the keyboard, then the K character appears next to the input prompt.
And the different characters will go on appearing, if you press any special key from the keyboard to come out to editor window.
The following program will show you what I want to say.
#include <stdio.h>
#include <conio.h>
main()
{
     int number;
     clrscr();
     printf(“Enter a number:”);
    scanf(“%d”,&number);
   getch();
}

The above program takes an integer number from the user and then does nothing.
Run the program. Input any number at input prompt like this:
Output:
#-----------------------------------------------------------------------------------------#
Enter any number: 12 <Enter>

#-----------------------------------------------------------------------------------------#

Now instead pressing Enter again press Up Arrow key. Now run the program again. Your input prompt is now as shown below:

#-----------------------------------------------------------------------------------------#
Enter any number: H
#-----------------------------------------------------------------------------------------#

I think, you got my point now.
In the above program we used scanf() function and type declaration section. We will see detailed about scanf() in the next section.

For a time being, remember that, if you press a special key from the keyboard while using getch(), it will return 0 and program will continue. But, this 0 will be stored in a buffer and will be returned from the next call to getch(). Thus the getch() will not work. The following program clarifies this concept.

#include <stdio.h>
#include <conio.h>
main()
{
      float radius,area;
      clrscr();
      printf(“Money is not everything.”);
      getch(); /* Press any special key here say F1 at the output prompt and notice that the second getch()        
                          will not work.*/  
      printf(“\nThere is Master Card and Visa.”);
      getch();
}
Run the above program and press any special key, say F1. When the message “Money is not everything.” is displayed and you will observe that the second getch() will not work (i.e. our program output does not pause after the message “There is Master card and Visa.”). We can rectify this limitation using the program below.

#include <stdio.h>
#include <conio.h>
main()
{
      float radius,area;
      clrscr();
      printf(“Money is not everything.”);
      if(getch()==0)
          getch();
      printf(“\nThere is Master Card and Visa.”);
      if(getch()==0)
          getch();
}

Now, if the user presses any special key, then the if statement becomes true and the value returned by getch() (i.e. 0) is passes to the extra getch() and so the program works normally. If the input key is not a special key, then the if statement becomes false and the program behaves like simple program. Note that the statement if(getch()==0) pauses the program and also check whether the value returned by the getch() is 0 (zero) or not.

No comments:

Post a Comment