2

To this code:

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>

using namespace std;

#define p(a) cout << "|" << a << "|" << endl
int main() {
    
    char a[100];
    char b[100];

    cin.getline(a, 4, '\n');

    cin >> b;

    p(a);
    p(b);
    
    return 0;
}

If my input is "1234"

1234

I have the next output:

|123|
||

but I know that with getline, I get three elements: "123" and it is sending to a variable adding to the end the \0 character. In the same way, the remain characters "4\n" is left in the buffer. Then Why in the next "cin>>" it is not copied to b variable?, Why b is null?

Kintaro Oe
  • 161
  • 1
  • 5

1 Answers1

6

If std::istream::getline can't fit the whole line (and terminating null) into the given buffer, it sets an error flag and becomes unreadable until the error is cleared.

std::istream::get's fourth overload is a better choice for this use-case as it doesn't raise a flag if the whole line is not consumed.

Replace

cin.getline(a, 4, '\n');

with

cin.get(a, 4, '\n');

Unrelated Recommendation:

After any IO transaction, test the state of the stream before proceeding so that youy know if it's worth proceeding as planned or if the transaction failed and you'll have to do something else (like clear the error and try it again). For example, you can

if (cin)
{
    // safe to act upon the values read
}

or, because almost all stream functions return the stream for easy chaining of IO,

if (cin.get(a, 4, '\n'))
{
    // safe to act upon the values read
}

Since I mentioned it, chaining is the magic that allows code like

std::cin >> a >> b >> c;

and with a test for successful reading of a, b, and c

if (std::cin >> a >> b >> c)
{
    // safe to do stuff with a, b, and c
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Nice, where do you find this info? "it sets an error flag and becomes unreadable until the error is cleared" – artm Aug 19 '20 at 23:34
  • @artm third bullet point down in the discussion of the function behaviour at the first link in the answer: *`count-1` characters have been extracted (in which case `setstate(failbit)` is executed).* – user4581301 Aug 19 '20 at 23:37
  • @artm you can clear the error with `cin.clear()` but then you don't know why it failed. – Jerry Jeremiah Aug 19 '20 at 23:45
  • @user4581301 I think I need to learn more about C++, in order to know these details, which book or pdf do you recommend reading? how did you know that?, thanks – Kintaro Oe Aug 20 '20 at 01:28
  • @KintaroOe [Stack Overflow maintains a curated list of widely respected text and reference books as well as some online resources.](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Aug 20 '20 at 02:20
  • I deliberately avoided mentioning `clear` here because I figure it's better to use the right tool than clean up after the wrong one. – user4581301 Aug 20 '20 at 02:21