C++ Primer Plus 学习笔记 第十七章 cin

cin支持的类型

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

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

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

#include <iostream>

int main()
{
  using namespace std;
  cout << "Enter numbers: ";

  int sum = 0;
  int input;
  while (cin >> input)
  {
    sum += input;
  }

  cout << "Last value entered = " << input <<endl;
  cout << "Sum = " << sum << endl;
  return 0;
}

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 <iostream>
#include <exception>

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 <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');

  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 <iostream>

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 <iostream>
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的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@凌晨三点半

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值