search

Home  >  Q&A  >  body text

c++11 - c++输入字符的问题

我是想输入一句话进去,英文的。这样就需要保存住单词之间的空格,所以就是保存字符。我这个代码编译的时候没有错误,但是运行时,我输入一句话之后按回车,并不会输出我刚输入的内容。没有反应。请大家看一下。谢谢。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    vector<char> ch;
    char word;
    int i;
    cout << "enter a sentence:" << endl;
    while (cin >> word)
    {
        ch.push_back(word);
    }
    for(auto c : ch)
        cout << c <<endl;
    system("pause");
    return 0;
}

伊谢尔伦伊谢尔伦2774 days ago418

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 13:51:02

    The poster wants to find out

    while (cin >> word)
    
    The end condition of

    is not that you enter the "newline character", but the "end of file character" (EOF), which is ctrl+D on Linux and ctrl+Z on Windows. Therefore, your program cannot input one line and output one line. Everything you input is successful, but it is saved in word and no output statement is executed.

    reply
    0
  • Cancelreply