cout << "Hello World"; // "Hello World" is printed entirely
cout << "Hello " << "World"; // "Hello " is printed first, then "World" is printed second
So the final result is the same, but in the first case, the message is given at once.
The second case is used when you need to put something in between them:
int main()
{
int num;
cout << "Enter number";
cin >> num;
if ( num < 0)
{ cout << "You have a positive number\n";
}
else
{ cout << "You have a negative number\n;
}
}
The second code contains mistakes and will not compile.
Lines 6, 8, 9, 11.
To answer the question, from what I see the author didn't have a true reason to split the message and "\n".
Maybe the author thought he would make the message easier to read.
Maybe he originally used std::endl and then decided to replace it with "\n".
1 2 3 4 5 6 7 8 9 10 11
// std::endl will put a '\n' then flush the stream,
// and so it shouldn't be overused
cout << "Correct details" << endl;
cout << "Incorrect details" << endl;
// author realizes this is clumsy...
// and does a Replace endl with "\n"
cout << "Correct details" << "\n";
cout << "Incorrect details" << "\n";