简体   繁体   中英

cin.getline( ) with larger size

#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?

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

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

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

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;

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM