search

Home  >  Q&A  >  body text

C++: Enter an article without giving the number of words. Use a cin loop to get each word. How to stop the loop at the end of the article?

Enter an article without giving the number of words. Use a cin loop to get each word. How to stop the loop at the end of the article?
For example:
Input: to be or not to be is a question
Code:

string a;
while( ? ) //无法停止循环
{
    cin >> a;
    // Do some thing...
}
曾经蜡笔没有小新曾经蜡笔没有小新2755 days ago1346

reply all(2)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-07-04 13:47:46

    while(cin >> a)
    {
        // Do some thing...
    }
    
    cin在读取输入流的末尾是会设置标记位failbit为false

    reply
    0
  • 某草草

    某草草2017-07-04 13:47:46

    if(a == "quit")
        break;

    reply
    0
  • Cancelreply