cin.getline( ) with larger size

谁说我不能喝 提交于 2021-02-04 12:42:46

问题


#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;
}

When I input:

12345678901234567890

output is:

123456789

It seems cont is empty. Could someone explain it?


回答1:


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;
}



回答2:


If the variable you read into isn't big enough to hold the entire line, the input operation fails and the stream will not read anything more until you call cin.clear().

You should use a std::string instead to hold the data. It will resize itself to match the input.

std::string test;
std::string cont;

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



回答3:


The standard says that you can get a "shorter" line than that you entered under the following conditions:

  1. The C++ you use may not be confirming to the standard. - not possible.
  2. You hit an EOF like character somewhere.

My guess would be to change the char[] to a string (STL) and then try it out. Also, when you say you input 12345678901234567890 in one go, all of it goes into test. And since test is only 10 bytes long, 123456789 would be output. Nothing is input into cont since the failbit is set for the istream class and further input is prevented. This code works for me with std::string.

    #include<iostream>
    #include <string>
    using namespace std;

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

       string test;
       string cont;

       cin >> test;
       cin >> cont;

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

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

}



回答4:


Copied from somewhere

cin.getline Extracts characters from the input sequence and stores them as a c-string into the array beginning at s.

Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delim if this parameter is specified, or '\n' otherwise). The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.

I believe you pressed enter twice after entering 12345678901234567890




回答5:


The getline() function has the following two syntaxes:

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

s: Pointer to an array of characters where extracted characters are stored as a c-string.

n: Maximum number of characters to write to s (including the terminating null character).

delim: Explicit delimiting character

The return type of this function is istream object (*this).

In the above scenario, the data is read into the pointer to an array of character, test, which is converted at runtime and hence can store up to 50 characters as declared in the cin.getline(test, 50).

If you want to achieve your desired result kindly use n=10



来源:https://stackoverflow.com/questions/6053957/cin-getline-with-larger-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!