cin.getline( ) with larger size
Asked Answered
P

5

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

Proximate answered 19/5, 2011 at 4:57 Comment(1)
Do everybody a favor and forget that cin.getline even exists. Use std::getline instead. It isn't perfect, but for most situations, it's a definite improvement.Fahy
M
11

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;
}
Malamut answered 19/5, 2011 at 5:11 Comment(1)
Thanks, "istream::getline sets the fail bit if the input is too long, and that prevents further input." is really what I want to hear.Proximate
H
6

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);
Husk answered 19/5, 2011 at 5:16 Comment(1)
Your answer is also excellent. Thanks. cin.clear() is important to solve the problem.Proximate
U
0

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;

}
Ulcer answered 19/5, 2011 at 5:7 Comment(0)
V
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

Virulence answered 19/5, 2011 at 5:10 Comment(0)
F
0

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

Fez answered 5/8, 2017 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.