在C ++中使用getline函数连续读取两行 [英] Reading two line continuously using getline function in c++

查看:755
本文介绍了在C ++中使用getline函数连续读取两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用getline函数读取字符串.

I was learning how to read strings with getline function.

我知道,只要我们不按Enter键,或者getline参数中的size值会交叉,getline函数就会读取字符串.就我尝试使用getline函数读取一行字符串而言,我没有遇到任何问题.

I know that getline function read string as far as we don't hit enter or the size value in the getline parameter go cross. As far as I tried getline function to read one line of string I had not faced any problem.

但是当我试图在两个不同的char数组中一个接一个地读取两行字符串时,得到了我不期望的输出. 要了解我的问题,请遵循波纹管

But when I was trying to read two line of string one after another in two different char array i got the output that was not expected to me. To understand my question follow bellow lines

#include <iostream>
using namespace std;
int main()
{
    char line1[10];
    char line2[10];
    cin.getline(line1,7);
    cin.getline(line2,7);
    cout << "\nline1 =" << line1 <<endl;
    cout << "line2 =" << line2 <<endl;
}

当我运行上面的程序时,它要求我输入,然后我以橙色作为第一个输入,然后按Enter键.

When I ran the above program it ask me for input then I gave orange as first input and hit the enter button.

接下来,它要求我提供第二个输入.然后我给了香蕉,然后按了回车按钮.在这种情况下,它会产生我期望的结果.但是如果为第一个输入输入橙色,它不会等我输入第二个输入.

Next it ask me to give the second input .then i gave banana and hit the enter button .in this case it produce the result i expected .But if enter oranges for the first input it does not wait for me to enter the second input.

因此,第1行存储为橙色,但第2行保持空白. 现在我的问题是第1行存储橙色没有错.但是我不明白为什么line2保持空白,否则它不应该包含line1接受输入后剩余的数据,我的意思是line2不应包含s作为值.

As a result line1 store orange but line2 remains blank. Now my question is that there is no wrong with line1 storing orange. But I don't understand why the line2 remains blank should not it contain the data that remains after line1 take input I mean should not line2 contain s as value.

因为橙色是6位数字的单词,所以getline将存储前6位数字,然后在设置明胶7的大小时添加一个空字符.

Because orange is a 6 digit word so getline will stores the first six digit after then a null character will be added as I set the size of geline 7.

接下来将在getline函数的下一次调用中获取其他剩余数据.因此,不应像第一次读取new_line字符后那样将其存储在line2中.

Then other remaing data will be assigend in the next call of getline function.So should not s stored in line2 as after s a new_line character is read for the first time.

为什么第2行保持空白,为什么在输入第一个输入后屏幕仍不停止输入?

Why will be line2 remain blank and why the screen doesn't stop for taking input after giving the first input?

推荐答案

std::istream::getline正在数据超载.

根据cppreference,

表现为 UnformattedInputFunction .在构造并检查了哨兵对象之后,从* this中提取字符并将它们存储在数组的连续位置中,该数组的第一个元素由s指向,直到发生以下任何一种情况(按所示顺序进行测试):

Behaves as UnformattedInputFunction. After constructing and checking the sentry object, extracts characters from *this and stores them in successive locations of the array whose first element is pointed to by s, until any of the following occurs (tested in the order shown):

  • 文件条件结束按输入顺序出现(在这种情况下,将执行setstate(eofbit))
  • 下一个可用字符c是定界符,由Traits::eq(c, delim)确定.提取定界符(与 basic_istream :: get()不同)并计入gcount(),但未存储.
  • 已提取
  • count-1个字符(在这种情况下将执行setstate(failbit)).
  • end of file condition occurs in the input sequence (in which case setstate(eofbit) is executed)
  • the next available character c is the delimiter, as determined by Traits::eq(c, delim). The delimiter is extracted (unlike basic_istream::get()) and counted towards gcount(), but is not stored.
  • count-1 characters have been extracted (in which case setstate(failbit) is executed).

强调我的.

cin.getline(line1,7);
//                ^ This is count

只能读取6个字符,其中第7个保留为空终止符. 橙色"指的是橙色".是七个字符,这将cin置于无法读取的错误状态,必须先清除该错误状态,然后才能继续读取.阅读第二行

can read only 6 characters with the 7th reserved for the null terminator. "oranges" is seven characters, and this places cin in a non-readable error state that must be cleared before reading can be continued. Reading of the second line

cin.getline(line2,7);

立即失败,并且没有读取任何数据.

instantly fails and no data is read.

显而易见的解决方案是

cin.getline(line1, sizeof(line1));

利用整个数组.但是...

to take advantage of the whole array. But...

应该测试任何IO事务是否成功,所以

Any IO transaction should be tested for success, so

if (cin.getline(line1, sizeof(line1)))
{
    // continue gathering 
}
else
{
    // handle error
}

是更好的选择.

最好还是使用 std::getline std::string 几乎消除了大小限制.

Better still would be to use std::getline and std::string to almost eliminate the size constraints.

这篇关于在C ++中使用getline函数连续读取两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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