I was learning how to read strings with getline function.
I know that getline function read string as far as we don't hit enter or the size value in the getline parameter go cross. As far as I tried getline function to read one line of string I had not faced any problem.
But when I was trying to read two line of string one after another in two different char array i got the output that was not expected to me. To understand my question follow bellow lines
#include <iostream>
using namespace std;
int main()
{
char line1[10];
char line2[10];
cin.getline(line1,7);
cin.getline(line2,7);
cout << "\nline1 =" << line1 <<endl;
cout << "line2 =" << line2 <<endl;
}
When I ran the above program it ask me for input then I gave orange as first input and hit the enter button.
Next it ask me to give the second input .then i gave banana and hit the enter button .in this case it produce the result i expected .But if enter oranges for the first input it does not wait for me to enter the second input.
As a result line1 store orange but line2 remains blank. Now my question is that there is no wrong with line1 storing orange. But I don't understand why the line2 remains blank should not it contain the data that remains after line1 take input I mean should not line2 contain s as value.
Because orange is a 6 digit word so getline will stores the first six digit after then a null character will be added as I set the size of geline 7.
Then other remaing data will be assigend in the next call of getline function.So should not s stored in line2 as after s a new_line character is read for the first time.
Why will be line2 remain blank and why the screen doesn't stop for taking input after giving the first input?