cin 的方法处理字符串小作用

C++基础教程——从问题分析到程序设计(第2版)中地案例代码

#include <iostream>

using namespace std;

int main()
{
char ch;

cout << "Line 1: Enter a string: "; //Line 1
cin.get(ch); //Line 2
cout << endl; //Line 3
cout << "Line 4: After first cin.get(ch); "
<< "ch = " << ch << endl; //Line 4

cin.get(ch); //Line 5
cout << "Line 6: After second cin.get(ch); "
<< "ch = " << ch << endl; //Line 6

cin.putback(ch); //Line 7
cin.get(ch); //Line 8
cout << "Line 9: After putback and then "
<< "cin.get(ch); ch = " << ch << endl; //Line 9

ch = cin.peek(); //Line 10
cout << "Line 11: After cin.peek(); ch = "
<< ch << endl; //Line 11

cin.get(ch); //Line 12
cout << "Line 13: After cin.get(ch); ch = "
<< ch << endl; //Line 13

return 0;
}

输出显示:
Line 1: Enter a string: liaozy

Line 4: After first cin.get(ch); ch = l
Line 6: After second cin.get(ch); ch = i
Line 9: After putback and then cin.get(ch); ch = i
Line 11: After cin.peek(); ch = a
Line 13: After cin.get(ch); ch = a
Press any key to continue
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值