cin.getline()较大 [英] cin.getline( ) with larger size

查看:83
本文介绍了cin.getline()较大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>
using namespace std;

int main()
{
   char test[10];
   char cont[10];

   cin.getline(test,10);
   cin.getline(cont,10);

   cout<<test<<" is not "<<cont<<endl;
    return 0;
}

当我输入时:

12345678901234567890

12345678901234567890

输出为:

123456789

123456789

似乎 cont 为空.有人可以解释吗?

It seems cont is empty. Could someone explain it?

推荐答案

istream :: getline 会设置失败位,从而阻止了进一步的输入.将您的代码更改为:

istream::getline sets the fail bit if the input is too long, and that prevents further input. Change your code to:

#include<iostream>
using namespace std;

int main()
{
   char test[10];
   char cont[10];

   cin.getline(test,10);
   cin.clear();                    // add this
   cin.getline(cont,10);

   cout<<test<<" is not "<<cont<<endl;
    return 0;
}

这篇关于cin.getline()较大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆