流状态
//===============================================================
//FileName:
// cinexp.cpp
//Date:
// 2019/12/2
//Author:
// khoing(https://blog.csdn.net/qq_45391763)
//===============================================================
// cinexcp.cpp -- having cin throw an exception
#include <iostream>
#include <exception>
int main()
{
using namespace std;
//------------------------------------------------------------------------
//exceptions默认的参数设置为goodbit,没有引发异常,
cin.exceptions(ios_base::failbit);//返回一个位掩码,指出哪些标记导致异常被引发
//也可以设置多位
//cin.exceptions(ios_base::badbit | ios_base::eofbit);
//failbit表示输入操作未能读取预期的字符或输出操作没有写入预期的字符,设置为1
//badbit如果流被破坏,则设置为1,例如文件读取错误,
//eofbit如果到达文件尾,则设置为1
//clear和setstate重置状态
//clear()使用默认参数0,这将清除全部3个状态位eofbit、badbit、failbit(设置为0)
//参数设置为eofbit(设置为1), 那么其他两位的状态位将被清除(设置为0)
//setstate方法只影响其参数中已设置的位。
//在输入不匹配,需要使用不带参数的clear重新打开输入
//------------------------------------------------------------------------
cout << "Enter numbers: ";
int sum = 0;
int input;
//------------------------------------------------------------------------
try {
while (cin >> input)//当输入非数字时将会引发catch中
{
sum += input;
}
}
catch (ios_base::failure & bf)
{
cout << bf.what() << endl;
cout << "O! the horror!\n";
}
cout << "Last value entered = " << input << endl;
return 0;
}
istream方法
单字符输入
get
get()
方法读取下一个输入单字符,即使该字符是空格、制表符、换行符。返回整形。通常为int。但是可以使用char ch = cin.get()。
到达文件尾时,get()
将返回EOF,
int a;//这里为int类型,而不是char,因为EOF值无法使用char类型来表示
while((a = cin.get()) != EOF){
//process
}
get(char &ch)
将输入字符赋给其参数。返回一个指向他的istream对象的引用,可以cin.get(c1).get(c2)>>c3;
而get()方法不行。可以在 最后使用get(),如:cin.get(c1).get()
>>
,get(char &)
,get()
使用哪一个?- 应确定是否希望跳过空白,则使用
>>
- 检测每个字符用
get(char &)
,get()
字符串输入
istream & get(char* , int , char);
istream & get(char* , int );
istream & getline(char* , int , char);
istream & get(char* , int );`
- 参数1:用于放置输入字符串的内存单元的地址
- 参数2:比要读取的最大字符数大1(用于存储结尾的空字符,以便将输入存储为一个字符串)
- 参数3:指定用作分界符的字符,只有两个参数的版本将换行符用作分界符
char line[50]; cin.get(line,50);
到达地49个字符或遇到换行符停止将输入读取到数组中。 - get和getline区别:
- 这两个函数都读取一行输入,直到到达换行符。
- get将换行符留到输入流中,这样接下来的输入操作首先看到的是换行符。
cin.get(ch,4); cin.get()
- 第二句会有问题,没有第二次输入,因为第一次会把换行符留到输入队列中,因此第二次调用时,看到的第一个字符便是换行符,此
get()
会认为已到达行尾,而没有发现任何可读取的内容。
- getline抽取并求丢弃输入流中的换行符。
- get将换行符留到输入流中,这样接下来的输入操作首先看到的是换行符。
//===============================================================
//FileName:
// get_gun.cpp
//Date:
// 2019/12/2
//Author:
// khoing(https://blog.csdn.net/qq_45391763)
//===============================================================
// get_fun.cpp -- using get() and getline()
#include <iostream>
//------------------------------------------------------------------------
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'); // discard rest of line
//参数1: 一个是数字,指定要读取的最大字符数
//参数2:用作输入分界符。
//丢弃接下来的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;
//getline函数将丢弃输入中的分界字符#,而get()函数不会。
return 0;
}