what is the actual use of getch () in c?.
pls, explain me.
what is the actual use of getch () in c?.
pls, explain me.
I use getch() in programs which needs something like this:
I use getch since my comiler supports it.Code:#include <stdio.h> #include <stdlib.h> int main () { int ch; while ((ch = getch()) && ch != '\r') { putchar ('*'); } system("Pause"); }
Note that if you try to use '\n' to control the loop it will not work. If you want input until user press "enter" use '\r'.
- Micko
Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem
Can you at least Google it?
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
Or search the FAQ. One result: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
getch() prompts the user to press a key, and returns the value of that key or alpha (value of 224) if it is not a standard key (such as if it is F1, or a numberpad key). Return equates to '\r' but Alt+Return equates to '\n'. If alph is returned, a second call to getch() will return a value corisponding to the key pressed. For example the up arrow returns 'K' the second time getch() is called.
Edit, forgot to mention: getch(), does not print the input to the screen. If you want to echo what is typed, use getche().
Very powerfull function.
Last edited by King Mir; 06-21-2006 at 05:40 PM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Do you just make stuff up, or do you really think you know what you're talking about? It doesn't "prompt" for anything. It simply reads a key, just like getchar does, except for the fact that getchar is buffered input, and is a standard function.
Quzah.
Hope is the first step on the road to disappointment.
Perhalps prompt is not the right word. I ment to convey that it will not read from stdin buffer, and thus will always ask for a key, unlike getchar() which will not ask for a key if stdin has something in it. Actually, as I explained it will not ask for a key if the last key returned was alpha.Originally Posted by quzah
The rest is valid. Test it yourself if you do not believe me.
Getch() is much more than just an unbuffered getchar().
Last edited by King Mir; 06-21-2006 at 05:28 PM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
No, not really it isn't. Oh, other than the fact that getch isn't standard, so there are multiple behaviors for it. Compare the <conio.h> version with the <curses.h> version. But no, there really isn't any difference other than the buffering. Even then, the curses version will function very similar to the standard getchar if you set it up right. Your "alpha" as you call it, is nothing special.Originally Posted by King Mir
Quzah.
Hope is the first step on the road to disappointment.
I don't know the cruses version, but the conio version works like this:
Furthermore, this is exactly how my IDE, DevC++ 4.9.9.2 works. I have used the function extensively.Originally Posted by iRMX® C Library Reference
0xe0 is the ascii value that prints out as alpha. That is a special value.
Getch will function as getchar for normal keys, including alphanumeric keys, escape, and a few others. F-keys; numberpad keys; Curser keys; Home, Pg Up, and simmilar keys; delete; and numberpad alternate keys all print out alpha first.
Last edited by King Mir; 06-21-2006 at 07:46 PM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
It is probably a bad idea to tell the OP how your specific compiler implements getch() since this information is useless unless he uses the same compiler and programs for the same platform that you do.
Telling the OP that getch() is an unbuffered getchar() is a correct answer that doesn't make any wrong assumptions about the person asking.
First of all, DevC++ is a very common IDE, so it is fully relivant how it implements getch().
Second, although RadiSys Corporation is not the foremost athority on the C librairy, their guide seems to be a valid summary of usefull C functions, and it has not been wrong for me yet. This reference is not part of DevC++ or the Mingw compiler.
Last edited by King Mir; 06-21-2006 at 07:53 PM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
So exactly what part of "reads a single character from the console" don't you understand? That is what getchar does also.
Quzah.
Hope is the first step on the road to disappointment.
From above:Originally Posted by iRMX® C Library Reference
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
How can you get the value of _getch() into a variable?
Var = _getch();
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.