什么是营销型的网站推广,开学第一课汉字做网站,北海做网站的网络公司,织梦网站数据下载前言#xff1a;前面学习了while语句后#xff0c;下面用while语句实现一个重要的功能#xff0c;逐字符的读取键盘输入的字符序列#xff0c;并输出到显示屏上。
准备知识#xff1a; C的输入输出包含以下3方面的内容#xff1a; 对系统指定的标准设备的输入和输出。即…前言前面学习了while语句后下面用while语句实现一个重要的功能逐字符的读取键盘输入的字符序列并输出到显示屏上。
准备知识 C的输入输出包含以下3方面的内容 对系统指定的标准设备的输入和输出。即从键盘输入数据输出到显示器屏幕。这种输入输出称为标准的输入输出简称标准I/O。以外存为对象进行输入和输出。例如从磁盘文件输入数据数据再输出到磁盘文件。以外存文件为对象的输入输出称为文件的输入输出简称文件I/O。对内存中指定的空间进行输入和输出。通常指定一个字符数组作为存储空间实际上可以利用该空间存储任何信息。这种输入输出称为字符串输入输出简称串I/O。 目录
1. 方法一
2. 方法二
3. 方法三
4. iostream类的成员函数get
重载函数1int get()
重载函数2int get(char ch)
cin.get(ch)与cin.get()对比 1. 方法一
使用cin对象和流提取运算符“”读取。
#include iostream
#include cstring
using namespace std;
int main()
{char ch;int count 0;cout Enter characters, enter # to quit: \n;cin ch;while (ch ! #){cout ch;count;cin ch;}cout endl count characters read\n;return 0;
}
程序分析程序功能是逐个读取输入的文本并在遇到#字符时停止读取输入。结束循环的条件是最后读取的一个字符是#该条件通过在循环之前读取一个字符进行初始化并在循环体结尾读取下一个字符进行更新。输出如下 从输出结果看程序读取到了#前除空格以外的所有字符总共检测了14个字符有4个空格被忽视了。原因在于cin读取char值时与读取其他基本类型一样cin将忽略空格和换行符。
另外程序为何可以输入一串字符序列而不是单个字符并且程序输入#字符后为何还可以输入字符。这是因为键盘输入的文本内容会先被cin对象存放在缓冲区当用户按下回车键后他输入的内容才会发送给程序当一串字符序列发送给程序后程序再逐个字符的检测是否为#字符如果是#字符就退出循环并输出检测过的所有字符。
2. 方法二
使用iostream类的成员函数get()逐字符的读取字符序列可以避免方法一中忽视空格的问题。
将方法一中的代码修改如下
#include iostream
#include cstring
using namespace std;
int main()
{int count 0;cout Enter characters, enter # to quit: \n;char ch cin.get();while (ch ! #){cout ch;count;ch cin.get();}cout endl count characters read\n;return 0;
}
程序输出如下可见输出结果中包含了字符序列中的空格并且最终检测的字符数目也包含了空格。 3. 方法三
还可以使用带参数的get成员函数
#include iostream
#include cstring
using namespace std;
int main()
{int count 0;cout Enter characters, enter # to quit: \n;char ch;cin.get(ch);while (ch ! #){cout ch;count;cin.get(ch);}cout endl count characters read\n;return 0;
}
4. iostream类的成员函数get
get函数在iostream类中有多个重载函数本文主要介绍无参数的get函数和有一个参数的get函数。
重载函数1int get()
无参数的get函数cin.get()表示从指定的输入流(通常是键盘)中提取一个字符(包括空格)如果读取成功则函数的返回值就是读入的字符如果遇到文件结束符号(EOF)则返回EOF。
char ch;
ch cin.get();
if (ch ! EOF) {cout You entered: ch endl;
} else {cout End of file reached. endl;
}重载函数2int get(char ch)
有1个参数的get函数其形参是char类型的引用。cin.get(ch)表示从输入流中读取一个字符并赋给字符变量ch如果读取成功则函数返回非0值(true)如失败则函数返回0值(false)。cin.get(ch) 更适合用于需要明确知道读取操作是否成功的场合。
char ch;
if (cin.get(ch)) {cout You entered: ch endl;
} else {cout End of file reached. endl;
}cin.get(ch)与cin.get()对比
属性cin.get(ch)chcin.get()传递输入字符的方式赋给参数ch将函数返回值赋给ch用于字符输入时函数的返回值istream类对象执行bool转换后为trueint类型的字符编码到达EOF时函数的返回值istream类对象执行bool转换后为falseEOF
使用带参数的get函数更符合对象方式一方面因为其返回值是istream对象意味着可以将它们拼接起来使用另一方面还可以通过返回值判断字符读取是否成功。如将字符串game4个字符分别赋值给四个字符变量ch1,ch2,ch3,ch4。可以用如下形式实现
#include iostream
#include cstring
using namespace std;
int main()
{int count 0;cout Enter characters, enter # to quit: \n;char ch1, ch2, ch3, ch4;cin.get(ch1).get(ch2).get(ch3).get(ch4);cout ch1ch1 \n ch2 ch2 \n ch3 ch3 \n ch4 ch4 \n endl;return 0;
}
输出如下 文章转载自: http://www.morning.wqfj.cn.gov.cn.wqfj.cn http://www.morning.fsqbx.cn.gov.cn.fsqbx.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.fqklt.cn.gov.cn.fqklt.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.njstzsh.com.gov.cn.njstzsh.com http://www.morning.mhnd.cn.gov.cn.mhnd.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.hxxwq.cn.gov.cn.hxxwq.cn http://www.morning.bhdtx.cn.gov.cn.bhdtx.cn http://www.morning.tbqbd.cn.gov.cn.tbqbd.cn http://www.morning.spfh.cn.gov.cn.spfh.cn http://www.morning.rcjyc.cn.gov.cn.rcjyc.cn http://www.morning.wcyr.cn.gov.cn.wcyr.cn http://www.morning.qxkcx.cn.gov.cn.qxkcx.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.xkhhy.cn.gov.cn.xkhhy.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.hjjfp.cn.gov.cn.hjjfp.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.tnhg.cn.gov.cn.tnhg.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.hnrdtz.com.gov.cn.hnrdtz.com http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.ylrxd.cn.gov.cn.ylrxd.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.lxfqc.cn.gov.cn.lxfqc.cn http://www.morning.lhptg.cn.gov.cn.lhptg.cn http://www.morning.yrbq.cn.gov.cn.yrbq.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.jrslj.cn.gov.cn.jrslj.cn http://www.morning.wmdqc.com.gov.cn.wmdqc.com http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.qxlhj.cn.gov.cn.qxlhj.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.kqblk.cn.gov.cn.kqblk.cn http://www.morning.rxhn.cn.gov.cn.rxhn.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.tkztx.cn.gov.cn.tkztx.cn http://www.morning.tqrxm.cn.gov.cn.tqrxm.cn http://www.morning.nzfjm.cn.gov.cn.nzfjm.cn http://www.morning.tldfp.cn.gov.cn.tldfp.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.zmyhn.cn.gov.cn.zmyhn.cn http://www.morning.ksbmx.cn.gov.cn.ksbmx.cn http://www.morning.wfysn.cn.gov.cn.wfysn.cn http://www.morning.ybgt.cn.gov.cn.ybgt.cn http://www.morning.krkwp.cn.gov.cn.krkwp.cn http://www.morning.krkwh.cn.gov.cn.krkwh.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.tzjqm.cn.gov.cn.tzjqm.cn http://www.morning.jbztm.cn.gov.cn.jbztm.cn http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.ltdxq.cn.gov.cn.ltdxq.cn http://www.morning.ltzkk.cn.gov.cn.ltzkk.cn http://www.morning.rwmp.cn.gov.cn.rwmp.cn http://www.morning.sjjtz.cn.gov.cn.sjjtz.cn http://www.morning.wmdqc.com.gov.cn.wmdqc.com http://www.morning.hjjkz.cn.gov.cn.hjjkz.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.rxcqt.cn.gov.cn.rxcqt.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.kgtyj.cn.gov.cn.kgtyj.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.knczz.cn.gov.cn.knczz.cn http://www.morning.fygbq.cn.gov.cn.fygbq.cn