Is it possible to write in c what I have paraphrased below?
while inkey != char(32) continue with operation A, else stop and do operation B
Is it possible to write in c what I have paraphrased below?
while inkey != char(32) continue with operation A, else stop and do operation B
It depends on your OS and Compiler.
If you're looking for something to test if there has been a key press, not to wait if there hasn't, and not to wait for a newline.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Checking whether there has been a keypress is not the issue.
I am looking for a similar statement as inkey , the equivalent of which statement doesn't appear to be in C.
Since, you will not tell us what inkey means to you there is no way to help you!
Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
It's a truly stupid question.
A little inaccuracy saves tons of explanation. - H.H. Munro
The function of interest isn't in C because of the nature of C. It was originally purpose built to write the UNIX operating system while translating the assembler source into a language operating near the assembler level without no CPU dependencies. As such, C doesn't really address application level interests beyond files and i/o as a language or standard library.
Instead, it relies upon third party libraries, and in your example case one that spans a diverse range of hardware. If the concept of inkey is to examine what current keyboard button is being held down, that is rather specific to a device like the PC. The original C viewed input as a specialized type of device opened as a file, which you know as stdin (or stdout for output), and doesn't function in a related way. Where, in the console, a keystroke is data sent through a serial connection, and is therefore a keystroke result, on a PC the notion is far more interesting, related to the fact that the keyboard hardware is more closely tied to the computer the software is running on. Most specifically, there are ways to detect combinations of keys being held, when they are pressed and when they are released. That can't happen using the stdin concept standardized for C.
Students tend to think of this as a shortsighted design of a language, if it misses some feature out of the box they're interested in, but factually that's inverted. C offers much deeper access to the hardware, and there are likely many libraries which could provide what you want, or you could make one.
A lot depends on the target operating system. The keyboard is a single resource controlled by the OS in combination with other concerns, like window focus and mouse I/O. Windows has much different support for that hardware than *Nix systems do.
I agree with stahta01: you need to state what exactly is this inkey thing, and not just assume that C programmers will know what you're talking about because you saw it from another programming language. Like Salem, I assumed that you had reading a keypress in mind, but you explicitly repudiated that, so what on earth is inkey? A quick search of the web for "inkey programming" brings up a QBasic "INKEY$ command, which checks to see if a user typed a key and provides the keypress to the program", but that's not what you're thinking of since you wrote that "Checking whether there has been a keypress is not the issue".Originally Posted by zach
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hallo zach!
May be if you work with Linux, you can use kbhit() from this example:
or this:Code:#include <stdio.h> #include <termios.h> #include <unistd.h> #include <fcntl.h> int kbhit(void) { struct termios oldt, newt; int ch; int oldf; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); oldf = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); if(ch != EOF) { ungetc(ch, stdin); return 1; } return 0; } int main(void) { int letter= 0; while(letter == 0) { letter = kbhit(); printf("Press a key: %d\n", letter); } printf("You pressed '%c'!\n", getchar()); return 0; }
Sorry here is a source not a real header.Code:#ifndef GETCH_H_INCLUDED #define GETCH_H_INCLUDED #include <stdio.h> #include <termios.h> #include <unistd.h> #include <fcntl.h> //for new version of kbhit //#include <sys/time.h> old version /// reads from keypress, doesn't echo int getch(){ struct termios oldt, newt; int ch; tcgetattr( STDIN_FILENO, &oldt ); newt = oldt; newt.c_lflag &= ~( ICANON | ECHO ); tcsetattr( STDIN_FILENO, TCSANOW, &newt ); ch = getchar(); tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); return ch; } void clpuf(void) { while (getc(stdin) != '\n') ; } /// reads from keypress, echoes int getche(void) { struct termios oldattr, newattr; int ch; tcgetattr( STDIN_FILENO, &oldattr ); newattr = oldattr; newattr.c_lflag &= ~( ICANON ); tcsetattr( STDIN_FILENO, TCSANOW, &newattr ); ch = getchar(); tcsetattr( STDIN_FILENO, TCSANOW, &oldattr ); return ch; } /*old: long kbhit(void) { struct timeval tv; fd_set read_fd; tv.tv_sec=0; tv.tv_usec=0; FD_ZERO(&read_fd); FD_SET(0,&read_fd); if(select(1, &read_fd, NULL, NULL, &tv) == -1) return 0; if(FD_ISSET(0,&read_fd)) return 1; return 0; } */ int kbhit(void) { struct termios oldt, newt; int ch; int oldf; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); oldf = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); if(ch != EOF) { ungetc(ch, stdin); return 1; } return 0; } #endif // GETCH_H_INCLUDED
But i think as an example it is okay
this is the idea
Code:do {something while inkey != space bar}
5 days later, we get to the FAQ.
https://faq.cprogramming.com/cgi-bin...&id=1043284392
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
With two little changes
of the last examples:
you have a similar effect.Code:#include <stdio.h> #include <termios.h> #include <unistd.h> #include <fcntl.h> int kbhit(void) { struct termios oldt, newt; int ch; int oldf; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); oldf = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); if(ch != EOF) { //ungetc(ch, stdin); return ch; } return 0; } void clpuf(void) { while (getc(stdin) != '\n') ; } int main(void) { int letter= 0; while((letter = kbhit()) != 32) { //if (letter != 0) clpuf(); printf("Press a key: %d\n", letter); } printf("You pressed '%c'= %d decimal\n", letter, letter); return 0; }
But i think on modern fast Systems it can be a risk to use a substitute for kbhit() like here.
For terminals like in Linux such a code is almost outdated.
Inkey is a quickbasic function.what inkey means to you
"without goto we would be wtf'd"