char ch;<br>cin>>ch
子主题
ch=cin.get()
返回值
cin.get()返回字符,将字符编码作为Int 返回值
cin.get()如何处理EOF
函数达到EOF,没有返回字符,cin.get()返回一个字符常量EOF(定义为-1)
EOF在头文件iostream中
cin.fail() == false 可替换为 ch != EOF
ch=getchar()
返回值
ditto ,相似,返回字符,将字符编码作为Int 返回值
cin.get(ch)
不能处理EOF
cin.get(char)函数达到 EOF 时候,不会将一个特殊的值赋给ch,<br>不会赋任何值。
区别总结
<span style="font-family: Arial; font-size: 14px; line-height: 26px; color: rgb(255, 0, 0);">cin.get()和cin.get(ch)之间的区别:</span><br style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: Arial; font-size: 14px; line-height: 26px; color: rgb(0, 0, 153);">属性 cin.get(ch) ch=cin.get()<br>传递输入字符的方式 赋给参数ch 将返回值赋给ch<br>用于字符输入时函数返回值 istream类对象(执行bool转换后为true) int类型的字符编码<br>到达EOF时函数的返回值 istream类对象(执行bool转换后为false) 符号常量EOF</span>
使用选择
<span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;">使用字符参数的版本更符合对象的方式,因为其返回值是istream对象。这意味着可以将它们拼接起来。如,下面的代码将输入中的下一个字符读入到ch1中,并将接下来的一个字符读入到ch2中:</span><br style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;">cin.get(ch1).get(ch2);</span><br style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;">函数cin.get(ch1)返回一个cin对象,然后便可以通过该对象调用get(ch2)。</span><br style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;">get()的主要用途是能够将stdio.h的getchar() putchar()函数转换为iostream的cin.get()和cout.put()方</span>