Unfortunately, getline discards the character used as a delimiter - which defaults to '\n' when one is not specifically provided like this
getline ( cin, str, '\n');. So just hitting enter will not work, because getline tosses out the '\n' at the end of the string, even if it is the only character that has been read.
You may want to try something like this:
CODE
#include <iostream>
using namespace std;
int main() {
string str;
int count=0;
cout << "Enter the string: ";
do {
getline(cin, str);
count++;
} while (!str.empty());
count--;
cout << "The number of lines entered is: "<<count<<endl;
return 0;
}
This code works as follows. The input is received in a do-while loop. When the user types in a string and hits enter, the line is counted. It then tests whether the string is empty using the empty() method, which returns true if the string has no characters in it. If the user typed something before hitting enter, the string will not be empty, so the loop continues. However, when the user just hits enter, the '\n' is discarded, so the string is now empty, and the loop exits. The counter will still increment if the user just hits enter, so there is a decrement step included after the loop, to get rid of this extra line.
And the above won't work using just a while loop, since the string starts out empty - the test condition of the while loop would be false, so it would never execute.
Hope that helps,
-jjh