Home > Back-end >  How to use a getch () to collect user input SPACE and ESC?
How to use a getch () to collect user input SPACE and ESC?

Time:10-03

Everyone a great god please take fly ah, how to use a getch () to collect user input SPACE and ESC?

CodePudding user response:

(1) getch (), getche () function
These two functions is what I read in a character on the keyboard, the call format for:
Getch ();
Getche ();
The difference between the two is: getch () function will not be read in echo characters on the display screen, and getche ()
Function will read the characters echoed to the screen,
Case 1:
# include
The main ()
{
Char c, ch.
C=getch (); From the keyboard/* not echo to read in a character string variable c */
Putchar (c); Output the characters/* */
Ch=getche ();/* from the keyboard back to read a character to character variable ch */
Putchar (ch);
}
Advantage of the characteristics of echo and echo, these two functions are often used in the process of interactive input complete suspension
, and other functions,
Example 2:
# include
The main ()
{
Char c, s [20];
Printf (" Name: ");
Gets (s);
Printf (" Press any key to continue... ");
Getch ();/* waiting for input Ren Yijian */
}

(2) the getchar () function
Getchar () function is also read in a character from the keyboard, front and back to show it with two functions
The difference between: getchar () function did not end until waiting for input and press enter, back to the front of all the input words
Operator will show them one by one on the screen, but only the first character as a function of the return value,
Getchar () function call format for:
getchar();
Example 3:
# include
The main ()
{
Char c;
C=getchar (); Until the end of the carriage return/* read from keyboard characters */
Putchar (c);/* show that enter the first character of */
Getch ();/* wait by Ren Yijian */
}
Example 4
# include
The main ()
{
Char c;
While ((c=getchar ())!='/n')/* each getchar () reads a single character in turn */
Printf (" % c ", c);/* in accordance with the same output */
Getch ();/* wait by Ren Yijian */
}





Besides the file tail getch (); The reason,



First of all, don't forget to use getch () must be introduced header file conio. J h, learn C language before, we always like to use at the end of the program along with it, use it to implement the program runs out the effect of suspension does not exit, without the words, in a TC2.0 environment after we use Ctrl + F9 compile and run the program was running out back to TC in the environment, we simply can not afford time to see the results, to see the result at this moment, we are going to press Alt + F5 to see the results back to DOS environment, this is very troublesome, and if at the end of the program with a line of getch (); Statements, we can eliminate DOS to see the results of this step, because the program is run out is not quit, but stop the screen at the end of the program, press any key to return to TC in the environment that we take a look at getch () what is the role, getch () is actually an input command, just like we use cin> Program will stop and wait for you input, and cin, getch () function receives a character from the keyboard and display and not put this character, that is to say, it's not after you press a key on the screen you press what, and continue to run the back of the code, so we can use it to implement in c + + "press any key to continue" effect, namely the program encountered in getch (); This line, it will pause the program, waiting for you to press any key to continue again after it receives the character key at the back of the code,
You might ask, why we can not at the end of the program in c + + with getch (), the explanation is that the software is always constantly updated, the place with bad, of course, to be correct, getch () at the end of the program, it is not assigned to any variable, so it is total garbage code, in this place has nothing to do with the program, c + + with that in mind, so is not exit in each time the program is running out, but to stop the screen automatically, and show "press any key... "Press any key to exit the call you, it's like c + + in its environment to run the program, at the end of the program automatically added a line of getch (); Statements, and statements in this line have also added a line in front of the output statements cout<& lt;" Press any key... "; To prompt you to program finished, press any key to continue, in fact we have compiled the program at the end of the program itself is not to stop, we can in the compilation of the Debug directory to find the compiled application (exe extension), double click on the run it in the folder, you will find that the screen flashes the ms-dos window was shut down, because the program is running is automatically quit, returned to the Windows environment, of course, if we run the program in the DOS environment, we can see see DOS screen directly in the program run results, because after the program is run not CLS,
There is a statement, and getch () is similar to getche (), it also need to introduce header file conio. H, where is the difference between them? Difference lies in the getch () there is no return, according to getche () returns a display, how do you say? I, for example, you will understand,
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# include
# include
Void main ()
{
Char ch;
for(int i=0; i<5; I++)
{
Ch=getch ();
Printf (" % c ", ch);
}
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Input/output here I was using a C function library, use C + + iostream. J h, this I etc. Will say again, first of all, it is a cycle of 5 times in a row to achieve five pause, wait for our input, we compile and run the program, suppose we input abcde respectively, the result is displayed on the screen is abcde, the abcde not in ch=getch (); In the output, we put the printf (" % c ", ch); This line to remove, you will find our press any key program is over 5 times, but nothing shows on the screen,
Then we put in the code getch () into getche () and see what is different, we still input abcde respectively, then the result is displayed on the screen is aabbccddee, we put the printf (" % c ", ch); This line again take out to see, shows the result is abcde, to show that the program execution ch=getche (); This statement when we enter key to return to display on the screen, with or without echo is the only difference between them,
Well, why don't we say again use c + + libraries, you can try to put this program into the form of c + + :
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# include
# include
Void main ()
{
Char ch;
for(int i=0; i<5; I++)
{
Ch=getch ();
cout}
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
You will find that the results are completely different, to be honest I was not sure how it is compiled to run, before I used it in c + + to realize the function of any key to continue, found the problem, if the getch (); There's a cout< behind; & lt;" . "; Cout< statement he would have to do the following; & lt;" . "; And then execute getch (); Realize the pause between two statements again, sometimes with a coutSome people would say, now that is a function of C in the library, it should be eliminated, we also study it, use it? But I found where they are still useful, otherwise I will not say so much here to waste everyone's time, I will give you an example, the procedure is as follows:
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# include
# include

Void main ()
{
Char ch='*';
While (ch=='*')
{
Printf ("/n press * continue to cycle, press the other key to exit!" );
Ch=getch ();
}
Printf ("/n quit!" );
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
We can add we want to be in the loop body function, press * continue to cycle in the program, other any key to exit, and the use of getch () the characteristics of the echo, we no matter by what, don't leave a mark on the screen, make our interface to achieve beautiful effect, if there's a better way to realize this function, I might not carry so much here, if you really have a better idea, please tell me, thank you!
Here I copy others on a web page a few examples are as follows:
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//example 1: this example is to illustrate the getch (), getche () the difference between
# include
# include

//here tell a little story: because the code is in the other person on a web page, others use C environment, may not be required conio. H header file
//can use getch (); (I don't know), also may be forget to write down, on a web page source code no # include This line,
//I let wife go to see this page, the wife copy on a web page code to c + + environment, cannot compile cry with me,
//ha ha, my lovely wife!

Void main ()
{
Char c, ch.
C=getch (); From the keyboard/* not echo to read in a character string variable c */
Putchar (c); Output the characters/* */
Ch=getche ();/* from the keyboard back to read a character to character variable ch */
Putchar (ch);
Printf ("/n/n ");
}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//case 2: this example is demonstrated in the process of interactive input complete suspension function
# include
# include
  • Related