博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ Primer Plus 学习笔记 第十七章 cin
阅读量:4127 次
发布时间:2019-05-25

本文共 3385 字,大约阅读时间需要 11 分钟。

cin支持的类型

也支持 signed char*, char*, unsigned char*

cin连续输入的话会跳过空格, 换行符, 制表符

会再不符合变量类型的输入流前面停止。并且将不符合的字符留在输入流中

#include 
int main(){ using namespace std; cout << "Enter numbers: "; int sum = 0; int input; while (cin >> input) { sum += input; } cout << "Last value entered = " << input <

input最后返回的是0  嗯。。。为啥不是-123 难道是将false转换成int型么

流状态

iostate流状态数据成员的数据类型

3个ios_base元素组成: eofbit, badbit, failbit.

都是1位

文件末尾 设置eofbit 

未能读取到预期的字符 设置failbit

I/O失败 设置failbit

其他无法判断的失败 设置badbit

设置状态

clear() 默认使用默认参数0 将3个状态位全部设置为0 或者传入特定的状态位排除掉(就是说这个参数不设置为0 其他都设为0)

clear(eofbit)

setstate(),必须制定参数 然后设置该参数位为0

exceptions()

#include 
#include
int main(){ using namespace std; cin.exceptions(ios_base::failbit); cout << "Enter numbers: "; int sum = 0; int input; try { while (cin >> input) sum += input; } catch(ios_base::failure & bf) { cout << bf.what() << endl; cout << "O! the horror!\n"; } cout << "Last value entered = " << input << endl; cout << "Sum = " << sum << endl; return 0;}

异常抛的和书中不一样 嗯。。我不想去管它

 

其他istream类方法

get()读取下一个字符 转成整形 到文件尾 返回EOF

get(char & c) 转成字符型 跳过空格, 回车键停止

返回istream对象的引用

用哪个?

 

getline读取到换行符之后会抽出来并丢弃它

cin.ignore() 用来丢弃指定字符前的指定的字符或指定数量的字符

cin.ignore(255, '\n') 丢弃255个字符或达到了第一个换行符前面的所有字符

程序示例

#include 
const int Limit = 255;int main(){ using std::cout; using std::cin; using std::endl; char input[Limit]; cout << "Enter a string for getline() processing:\n"; cin.getline(input, Limit, '#'); cout << "Here is your input:\n"; cout << input << "\nDone with phase 1\n"; char ch; cin.get(ch); cout << "The next input character is " << ch << endl; if (ch != '\n') cin.ignore(Limit, '\n'); cout << "Enter a string for get() processing:\n"; cin.get(input, Limit, '#'); cout << "Here is your input:\n"; cout << input << "\nDone with phase 2\n"; cin.get(ch); cout << "The next input character is " << ch << endl; return 0;}

空行不会阻止getline()设置failbit

读满字符 设置failbit

其他istream方法

 

read()读取指定数量字符存到指定变量中 。不会转为字符串。也就是说结尾不会加上'\0'

peek()读取下一个字符 但不抽取出来。字符依然在输入流中 用来做判断

gcount()返回最后一个不是通过>>读取的字符数 使用strlen()更快

putback() 将1个字符插到输入字符串中 该字符是吓一跳输入语句读取的第一个字符

#include 
int main(){ using std::cout; using std::cin; using std::endl; char ch; while(cin.get(ch)) { if (ch != '#') cout << ch; else {// 将‘#’写到cin输入流开头 cin.putback(ch); break; } } if (!cin.eof()) { cin.get(ch); cout << endl << ch << " is next input character.\n"; } else { cout << "End of file reached.\n"; std::exit(0); } while(cin.peek() != '#') { cin.get(ch); cout << ch; } if (!cin.eof()) { cin.get(ch); cout << endl << ch << " is next input character.\n"; } else cout << "Enter of file reached.\n"; return 0;}

peek()例子

#include 
const int SLEN = 10;inline void eatline() {while (std::cin.get() != '\n') continue;}int main(){ using std::cout; using std::cin; using std::endl; char name[SLEN]; char title[SLEN]; cout << "Enter your name: "; cin.get(name, SLEN);// 检查字符流中当前的字符 if (cin.peek() != '\n') cout << "Sorry, we only have enough room for " << name << endl; eatline(); cout << "Dear " << name << ", enter your title: \n"; cin.get(title, SLEN); if (cin.peek() != '\n') cout << "We were forced to truncate your title.\n"; eatline(); cout <<" Name: " << name << "\nTitle: " << title << endl; return 0;}

cin内容完结 这节也好多内容 都是cin的

转载地址:http://psepi.baihongyu.com/

你可能感兴趣的文章
如何实现a===1 && a===2 && a===3返回true?
查看>>
49个在工作中常用且容易遗忘的CSS样式清单整理
查看>>
20种在学习编程的同时也可以在线赚钱的方法
查看>>
隐藏搜索框:CSS 动画正反向序列
查看>>
12 个JavaScript 特性技巧你可能从未使用过
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(上)
查看>>
【视频教程】Javascript ES6 教程27—ES6 构建一个Promise
查看>>
【5分钟代码练习】01—导航栏鼠标悬停效果的实现
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(中)
查看>>
8种ES6中扩展运算符的用法
查看>>
【视频教程】Javascript ES6 教程28—ES6 Promise 实例应用
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(下)
查看>>
【web素材】03-24款后台管理系统网站模板
查看>>
Flex 布局教程:语法篇
查看>>
年薪50万+的90后程序员都经历了什么?
查看>>
2019年哪些外快收入可达到2万以上?
查看>>
【JavaScript 教程】标准库—Date 对象
查看>>
前阿里手淘前端负责人@winter:前端人如何保持竞争力?
查看>>
【JavaScript 教程】面向对象编程——实例对象与 new 命令
查看>>
我在网易做了6年前端,想给求职者4条建议
查看>>