1. 程式人生 > >c/c++,輸入一個字元

c/c++,輸入一個字元

getch()、getche()和getchar()函式
    (1) getch()和getche()函式
    這兩個函式都是從鍵盤上讀入一個字元。其呼叫格式為:
     getch();
     getche();
    兩者的區別是: getch()函式不將讀入的字元回顯在顯示螢幕上, 而getche()
函式卻將讀入的字元回顯到顯示螢幕上。
    例1:
     #include<stdio.h>
     #include<conio.h>
     main()
     {
      char c, ch;
      c=getch();     /*從鍵盤上讀入一個字元不回顯送給字元變數c*/
      putchar(c);    /*輸出該字元*/
      ch=getche();   /*從鍵盤上帶回顯的讀入一個字元送給字元變數ch*/
      putchar(ch);
     }
    利用回顯和不回顯的特點, 這兩個函式經常用於互動輸入的過程中完成暫停
等功能。
    例2:
     #include<stdio.h>
     #include<conio.h>
     main()
     {
      char c, s[20];
      printf("Name:");
      gets(s);
      printf("Press any key to continue...");
      getch(); /*等待輸入任一鍵*/
     }
    (2) getchar()函式
    getchar()函式也是從鍵盤上讀入一個字元, 並帶回顯。它與前面兩個函式
的區別在於: getchar()函式等待輸入直到按回車才結束, 回車前的所有輸入字
符都會逐個顯示在螢幕上。但只有第一個字元作為函式的返回值。
    getchar()函式的呼叫格式為:
     getchar();
    例3:
     #include<stdio.h>
     #include<conio.h>
     main()
     {
          char c;
          c=getchar();   /*從鍵盤讀入字元直到回車結束*/
          putchar(c);    /*顯示輸入的第一個字元*/
          getch();       /*等待按任一健*/
     }
例4
     #include<stdio.h>
     #include<conio.h>
     main()
     {
          char c;
          while ((c=getchar())!='\n')   /*每個getchar()依次讀入一個字元*/
          printf("%c",c);    /*按照原樣輸出*/
          getch();       /*等待按任一健*/
     }

-----------------------------------------------------------------------------------------------------------------------------------

getch()等應有標頭檔案#include<conio.h>,下面是解釋:

第一點:
你既然用了getch()函式,在前面就應有標頭檔案#include<conio.h>。因為:
conio是Console Input/Output(控制檯輸入輸出)的簡寫,其中定義了通過控制檯進行資料輸入和資料輸出的函式,主要是一些使用者通過按鍵盤產生的對應操作,比如getch()函式等等。
包含的函式
cgets(char *);
cprintf(const char *, ...);
cputs(const char *);
cscanf(const char *, ...);
inp(unsigned short);
inpw(unsigned short);
getch(void);
getche(void);
kbhit(void);
outp(unsigned short, int);
outpw(unsigned short, unsigned short);
putch(int);
ungetch(int);
void _Cdecl clreol (void);
void _Cdecl clrscr (void);
void _Cdecl delline (void);
int _Cdecl gettext (int left, int top, int right, int bottom,
void *destin);
void _Cdecl gettextinfo (struct text_info *r);
void _Cdecl gotoxy (int x, int y);
void _Cdecl highvideo (void);
void _Cdecl insline (void);
void _Cdecl lowvideo (void);
int _Cdecl movetext (int left, int top, int right, int bottom,
int destleft, int desttop);
void _Cdecl normvideo (void);
int _Cdecl puttext (int left, int top, int right, int bottom,
void *source);
void _Cdecl textattr (int newattr);
void _Cdecl textbackground (int newcolor);
void _Cdecl textcolor (int newcolor);
void _Cdecl textmode (int newmode);
int _Cdecl wherex (void);
int _Cdecl wherey (void);
void _Cdecl window (int left, int top, int right, int bottom);
har *_Cdecl cgets (char *str);
int _Cdecl cprintf (const char *format, ...);
int _Cdecl cputs (const char *str);
int _Cdecl cscanf (const char *format, ...);
int _Cdecl getch (void);
int _Cdecl getche (void);
char *_Cdecl getpass (const char *prompt);
int _Cdecl kbhit (void);
int _Cdecl putch (int c);
int _Cdecl ungetch (int ch);

第二點:
你沒弄清getch()的用法。謹記如下:
getch直接從鍵盤獲取鍵值,不等待使用者按回車,只要使用者按一個鍵,getch就立刻返回,getch返回值是使用者輸入的ASCII碼,出錯返回-1.輸入的字元不會回顯在螢幕上.getch函式常用於程式除錯中,在除錯時,在關鍵位置顯示有關的結果以待檢視,然後用getch函式暫停程式執行,當按任意鍵後程序繼續執行.