I've got following code:
system("CLS");
string title;
string content;
cout << "Get title." << endl;
get开发者_高级运维line(cin,title);
cout << "Get content." << endl;
getline(cin,content);
The problem is - application is not asking about tittle, I've got Get title, get content and then waiting for user input, it's not waiting for user input after get title.bDo I have to add any break or smth?
Or maybe, that isn't the best idea to read whole text line from user input?If you have a cin >> something;
call prior to your system()
call.
For example, taking input into an integer. When cin >> myintvar;
(or similar) then the integer is placed in myintvar
and the '\n' gets sent along in the stream. The getline
picks the \n
up as indicative of the end of a line of input, so it is effectively "skipped".
Either change the cin >>
to a getline()
or call cin.ignore()
to grab the '\n'
(or better, call a cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n' );
to flush the input buffer-- but be sure you're not throwing away valuable input in the process).
I'd bet that you have something like a menu for selecting options ( as a numeric type ) and after that you try to read the lines.
This happens because after std::cin read some value the remaining '\n' was not processed yet, the solution would be to include #include <limits>
and then put std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
before your getline(cin,title);
It is because when you use getline() it ignores the newline at the end of the line and feeds it into the input queue, so when your getline function is called for next time it encounters the newline character discarded by the previous getline() and so it considers that as end of your input string. So thats why it doesn't take any input from you. You could use something like this
getline(cin,title);
cin.get();
hope this works.
精彩评论