0
//Stores the line
string line;
//create a vector where each element will be a new line
vector<string> v;
int counter = 0;
//While we havent reached the end of line
while (getline(cin, line) && !cin.eof())
{
//get the line and push it to a vector
v.push_back(line);
counter++;
for(int i = 0; i <counter; i++)
{
cout<<v[i]<<endl;
}
}
return 0;
}
的問題是,怎麼來的,如果我輸入讓說:如何檢測文件結束在C++字符串輸入
Hello
World (end of file)
輸出只有:
Hello
世界不輸出它只輸出你好和世界如果我輸入
Hello
World
(end of file)
很抱歉,如果這是一個非常簡單的問題:/,但如果你有沒有線的一端與EOF結束的行,這我不能算出這個
只要'while(getline(cin,line))'就足夠了。不需要額外的檢查'!cin.eof()'。 –
只要做'while(std :: getline(...))'就足夠了。 ['std :: getline'](http://en.cppreference.com/w/cpp/string/basic_string/getline)函數返回流,它可以用作[布爾表達式](http:// en.cppreference.com/w/cpp/io/basic_ios/operator_bool),當有錯誤或文件結束時它會返回「false」。 –
您正在描述相同的情況:hello world(行尾)! – saadtaame