By MrFarnham

2019-08-13 19:28:02 8 Comments

I'm just wondering why this standard function is returning a char count of 9 for the sample code at

// cin.gcount example
#include <iostream>     // std::cin, std::cout

int main () {
  char str[20];

  std::cout << "Please, enter a word: ";
  std::cout << std::cin.gcount() << " characters read: " << str << '\n';

  return 0;

Please, enter a word: simplify

9 characters read: simplify

Why is this returned as 9 characters?


@Remy Lebeau 2019-08-13 19:57:09 is generally considered by many people to be a poor site for C++ documentation. You really should use instead.

For instance, cppreference's istream::getline() documentation states the following:

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

  • 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).

If the function extracts no characters (e.g. if count < 1), setstate(failbit) is executed.

cplusplus's istream::getline() documentation states the following instead:

The delimiting character is the newline character ('\n') for the first form, and delim for the second: when found in the input sequence, it is extracted from the input sequence, but discarded and not written to s.


The number of characters successfully read and stored by this function can be accessed by calling member gcount.

Which is a little misleading, as it implies that getline() only counts characters that are stored in the user's buffer, but the delimiter is not stored in the buffer and yet it still counts towards gcount().

@NathanOliver- Reinstate Monica 2019-08-13 19:31:39

Because of the enter key. When you press enter, a newline character ('\n') is entered into the stream. getline reads up to that newline, stores the text in the array, and then reads and discards the newline. Thus, when you read simplify, you actually read simplify\n which is 9 characters.

Related Questions

Sponsored Content

5 Answered Questions

26 Answered Questions

36 Answered Questions

[SOLVED] Why is "using namespace std;" considered bad practice?

21 Answered Questions

[SOLVED] Why should I use a pointer rather than the object itself?

  • 2014-03-03 11:54:16
  • gEdringer
  • 298615 View
  • 1534 Score
  • 21 Answer
  • Tags:   c++ pointers c++11

10 Answered Questions

8 Answered Questions

[SOLVED] How to convert a std::string to const char* or char*?

  • 2008-12-07 19:30:56
  • user37875
  • 892014 View
  • 865 Score
  • 8 Answer
  • Tags:   c++ string char const

10 Answered Questions

2 Answered Questions

2 Answered Questions

[SOLVED] Converting std::vector to char*

1 Answered Questions

[SOLVED] Trying to return char array

  • 2013-11-11 18:15:44
  • user2980207
  • 116 View
  • 0 Score
  • 1 Answer
  • Tags:   c++ arrays char

Sponsored Content