Archived

This topic is now archived and is closed to further replies.

kuphryn

Problem With std::getline And C++

Recommended Posts

kuphryn    210
Hi, I find there is a slight inconvenience with the std::getline function in C++. Here is an example: ----------------------------------------------------------- #include using std::cin; using std::cout; using std::endl; using std::getline; #include 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

Share this post


Link to post
Share on other sites