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
?