-
Problem With std::getline And C++
Hi,
I find there is a slight inconvenience with the std::getline function in C++. Here is an example:
-----------------------------------------------------------
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::getline;
#include <string>
using std::string;
int main()
{
string strTemp;
cout << "Enter a sentence: ";
getline(cin, strTemp);
cout << "\nYou entered: "
<< strTemp << endl;
return 0;
}
-----------------------------------------------------
If you run the code above, and hopefully I did not leave anything out, out will find that after you entered a sentence and press Enter, the "cursor" will go to the next line, but the program does not move onto "You have entered: " until you press Enter the second time. In other words, I have had problems with the getline function in term of having to press Enter twice to get it to move onto the next algorithm.
I have tried for example:
getline(cin, strTemp, '\n');
It has the same effect.
Is there a way to get around having to press Enter twice?
Thanks,
Kuphryn
-
of Zen Hall
Worked for me. What compiler are you using?
-
I am using VC++ 6 under XP.
Kuphryn
-
of Zen Hall
Thought so, there's a fix here.
-
Interesting. Thanks.
I looked in string.h, but I could not find the line that article mentioned.
Kuphryn
-
of Zen Hall
string.h is the C string standard library header. STL string is just string (without the h extension).
-
Oh oh! Thanks!
It works great.
Thanks again.
Kuphryn
P.S. VC++ 6 is beginning to look pretty BAD for me.
-
_B-L-U-E_
Move this to the FAQ. Good stuff.
Popular pages
Recent additions