It uses line buffering -- you only get keystrokes after an entire line has been typed in. getch returns a single character typed (and without echoing it).Is there some reason not to use the standard C function: int getchar(void)?
Is there some reason not to use the standard C function: int getchar(void)?
There is the fact that it is NOT part of any standard!
If I remember correctly (and I may not) it was a Borland provide routine on DOS/Windows.
You have it backwards. getch() was by Borland. getchar() is in the Standard C Library and equivalent to getc(stdin).
Thanks a lot! The only problem is that it tells me that the identifier "read" is undeclared so the program does not work... Would it be because of a library or something?This is how I implemented getch for a ported MS/DOS program (!) that needed it. As a bonus, this also works for Linux.
#include <stdlib.h>
#include <sys/ioctl.h>
#include <termios.h>
int getch(void) {
char chbuf[1];
struct termios oldstate, newstate;
tcgetattr(0, &oldstate);
newstate = oldstate;
newstate.c_lflag &= ~ICANON;
newstate.c_lflag &= ~ECHO;
tcsetattr(0, TCSANOW, &newstate);
read(0, &chbuf, 1);
tcsetattr(0, TCSANOW, &oldstate);
}
Thanks a lot! The only problem is that it tells me that the identifier "read" is undeclared so the program does not work... Would it be because of a library or something?
It shouldn't as long as the correct header file is specified. The man page says you need:I am working on c++ does it change something?
- (void) controlTextDidChange:(NSNotification *) aNotification;
NSString * myString = [TextFieldName stringValue];
Since this is a Mac programming forum, the right way to do this is to create an XCode project with a single window. Add an NSTextField to the window. Create a custom class to be the window.xib's file owner. Create a delegate methodNSTextField delegate method and link to it. Every time a user types a character this method will be invoked. In the code for this method you can get the string from the text field using a call to some method likeCode:- (void) controlTextDidChange:(NSNotification *) aNotification;
Code:NSString * myString = [TextFieldName stringValue];
This is very oversimplified but it's really the way to get user input for the Mac.
Yes, you can create a console app as well.