Using getline is tripping you up here. The (usually undesirable but in this case useful) default behavior of cin is to read up until the first whitespace:
Code:
#include <iostream>
int main()
{
char s[64];
std::cout << "Enter a line of text:\n";
std::cin >> s;
std::cout << "\nYou entered: " << s << std::endl;
}
Code:
Enter a line of text:
one two three
You entered: one
Anything not read remains on the buffer. You can use additional reads to continue taking (whitespace delimited) words from the input buffer:
Code:
#include <iostream>
int main()
{
char s1[64];
char s2[64];
char s3[64];
std::cout << "Enter a line of text:\n";
std::cin >> s1;
std::cin >> s2;
std::cin >> s3;
std::cout << "\nYou entered:"
<< "\n> " << s1
<< "\n> " << s2
<< "\n> " << s3 << std::endl;
}
Code:
Enter a line of text:
one two three
You entered:
> one
> two
> three
Newlines are considered whitespace, so the same rules apply whether you enter a single line or three separate lines:
Code:
Enter a line of text:
one
two
three
You entered:
> one
> two
> three
The second program expects three words, whether they are separated by spaces or newlines (i.e. on separate lines).
So the notion of "lines" isn't really relevant in this situation. As long as there are more reads waiting to be executed, words will be extracted one at a time, whether separated by spaces or newlines. How do you continue calling cin to read the next word? Loop.