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函数暂停程序运行,当按任意键后程序继续运行.

c/c++,输入一个字符 2014-11-20 07:00 30人阅读 评论(0) 收藏的更多相关文章

  1. 网上关于sort结构体排序都不完整,我来写一个完整版的 2014-08-09 16:50 60人阅读 评论(0) 收藏

    主要参考sort函数_百度文库, 但是那篇有错误 2.结构体排序,a升,b降,c降 平板视图 打印? 01 #include <iostream> 02 #include <algo ...

  2. MS SQLServer 批量附加数据库 分类: SQL Server 数据库 2015-07-13 11:12 30人阅读 评论(0) 收藏

    ************************************************************ * 标题:MS SQLServer 批量附加数据库 * 说明:请根据下面的注释 ...

  3. DataGridView 列大写、列只能输入数字 分类: DataGridView 2014-12-07 08:40 332人阅读 评论(0) 收藏

    列大写: 说明:调用EditingControlShowing事件 private void dgvGoods_EditingControlShowing(object sender, DataGri ...

  4. PAT甲 1008. Elevator (20) 2016-09-09 23:00 22人阅读 评论(0) 收藏

    1008. Elevator (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The highest ...

  5. winform只允许一个应用程序运行 2014-12-08 09:51 31人阅读 评论(0) 收藏

    使用互斥体Mutex类型 导入命名空间 using System.Threading; //声明互斥体 Mutex mutex = new Mutex(false, "ThisShouldO ...

  6. c++map的用法 分类: POJ 2015-06-19 18:36 11人阅读 评论(0) 收藏

    c++map的用法 分类: 资料 2012-11-14 21:26 10573人阅读 评论(0) 收藏 举报 最全的c++map的用法 此文是复制来的0.0 1. map最基本的构造函数: map&l ...

  7. hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏

    hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...

  8. 【JAVA编码专题】总结 分类: B1_JAVA 2015-02-11 15:11 290人阅读 评论(0) 收藏

    第一部分:编码基础 为什么需要编码:用计算机看得懂的语言(二进制数)表示各种各样的字符. 一.基本概念 ASCII.Unicode.big5.GBK等为字符集,它们只定义了这个字符集内有哪些字符,以及 ...

  9. ZOJ2748 Free Kick 2017-04-18 20:40 40人阅读 评论(0) 收藏

    Free Kick Time Limit: 2 Seconds      Memory Limit: 65536 KB In a soccer game, a direct free kick is ...

随机推荐

  1. string.Format 格式化时间,货币

    1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...

  2. 查找所有含有表名(abc)的存储过程 执行脚本

    SELECT obj.Name , sc.TEXT FROM syscomments sc INNER JOIN sysobjects obj ON sc.Id = obj.ID WHERE sc.T ...

  3. Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)

    Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...

  4. Android Terminal telnet windows

    /******************************************************************************************** * Andr ...

  5. UPDATE语句中使用JOIN

    举个例子~ UPDATE e SET e.money = e.money + d.amount FROM employee e INNER JOIN ( GROUP BY empid) d ON d. ...

  6. 【C#学习笔记】函数调用

    using System; namespace ConsoleApplication { class Program { static int Add(int a, int b) { return a ...

  7. java线程join的意思(转自http://zjj1211.blog_51cto_com)

    Join,单词本事就是连接的意思. 先贴出几段代码猜猜结果. <1> public static int Main() { Alpha oAlpha = new Alpha(); Thre ...

  8. 【英语】Bingo口语笔记(57) - 常见的口语弱读

  9. Java核心技术II读书笔记(二)

    ch2 XML 有两种XML文档结构,DTD和Schema,用解释文档构成规则,这些规则指定了每个元素俺的合法子元素和属性. DTD DTD有多种提供方式,可以像下面这样加到XML中: <?xm ...

  10. 【转】session setup failed: NT_STATUS_LOGON_FAILURE -- 不错

    原文网址:http://blog.sina.com.cn/s/blog_5cdb72780100l26f.html samba服务器出现“session setup failed: NT_STATUS ...