c/c++,输入一个字符

来源:互联网 发布:河南建筑工程预算软件 编辑:程序博客网 时间:2024/03/29 00:32
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();       /*等待按任一健*/
     }

转自:http://hi.baidu.com/the_way_welike/blog/item/9a0aa28c5d31d918b31bba39.html

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

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函数暂停程序运行,当按任意键后程序继续运行.
0 0
原创粉丝点击