Search code examples
c++size

Why std::string.size() behaves abnormally in my code?


Here in this code, when I'm entering inputs one by one, I'm getting correct output as expected(correct size of string using std::string.size()), But when I'm entering three or four inputs together(or entering input in bulk) the output (size of string) gets incremented by 2.

Ps: Look once at attached output snippets.

#include <iostream>
int main()
{
int count;
std::cin >> count;

std::cin.ignore();

while (count--)
{

    std::string s;
    std::getline(std::cin, s);

    std::cout << s.size() << '\n';
}

return (0);
}

Output when I'm entering inputs one after another

Output when entering all input at once

Edit: I have printed all inputted string and found that at the end, the extra characters are two blank-spaces, as you can see below, (though I tried so far, but still don't know the reason):

   5 
   </h1>  
   </h1>  7
   Clearly_Invalid    
   Clearly_Invalid  17
   </singlabharat>    
   </singlabharat>  17
   </5>  
   </5>  6
   <//aA>

Solution

  • std::cin.ignore(); ignores characters to the EOF. When you enter one line, EOF is met. When you enter several lines, EOF is not met, but \n is met. Thus the next getline after the input 5 returns the empty string, the length is 0.

    When consuming whitespace-delimited input (e.g. int n; std::cin >> n;) any whitespace that follows, including a newline character, will be left on the input stream. Then when switching to line-oriented input, the first line retrieved with getline will be just that whitespace. In the likely case that this is unwanted behaviour, possible solutions include:

    • An explicit extraneous initial call to getline
    • Removing consecutive whitespace with std::cin >> std::ws
    • Ignoring all leftover characters on the line of input with cin.ignore(std::numeric_limitsstd::streamsize::max(), '\n');

    The other line lengths are increased perhaps by the pasting the text containing \r\n that are not processed by the MS specific text file input conversions in the middle.