[Dev-C++] getchar(), getch(), getche()
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <Dan...@ao...> - 2002-01-20 19:11:00
|
Lets try to clear the air about getch() Vs getchar():
They are both prototyped in conio.h, this file is located in the DEV-
CPP\INCLUDE folder.
getchar() is similar to C's gets() in as much as the variable is declared as
a char. When event trapping if the programmer wants to use numeric user input
then he/she will have to convert it to an integer using the atoi() function.
Also when the user presses a key the have to hit enter afterwards for the
variable to read.
getch() is totally different in as much as the variable is declared as an int
so the program can read both Alpha and Numeric results without any
conversions to be done on the programmers end. Also the user just has to type
in the required key and it will be automatically inputted.
Here is a brief example:
#include <conio.h>
void main(void)
{
int ch;
printf ("%s", "Press a key\n");
ch=getch() \\this starts the event tripping procedures
switch(ch) { //this tells the program how to handle the user input
case 'a':
printf("%s", "You pressed the A key\n");
break;
case 'b':
printf("%s", "You pressed the B key\n);
break
}
}
The last function in this message concerns getche() which performs the same
as getch() with one exception and that is that when the user presses a key
the keystroke is echoed to the screen. This is great for debugging a program.
In closing I'm working on a C tutorial that will include source code and
compiled examples. Any help with this project would be greatly appreciated.
Danny
|