Can’t we use cin.getline() inside a loop? I’m running the following code:
for(int k=0; k<t; k++)
{
char ph1[1000], ph2[1000], ph11[1000], ph22[1000][1000];
int i, j, count = 0;
cin.getline(ph1, 1000);
cin.getline(ph2, 1000);
}
The code is supposed to accept two strings (or char *) through cin.getline(). But when I run it, it accepts the first string, prints “NO” and exits. Why is it so? Why is the second cin.getline() being skipped?
Thanks in advance.
Even after you edited it, your example code still doesn’t compile. It’s a loop without surrounding
main().I rewrote it to:
This works as expected, i.e. the problem you perceive has nothing to do with “cin.getline() not working”, and is not exhibited by the code you posted. Voting for closing the question.