2011-09-28 37 views

回答

1

std::istream::getline(char *, streamsize)

被盗无耻地从http://www.cplusplus.com/reference/iostream/istream/getline/

// istream getline 
#include <iostream> 
using namespace std; 
int main() { 
    char name[256], title[256]; 

    cout << "Enter your name: "; 
    cin.getline (name,256); 

    cout << "Enter your favourite movie: "; 
    cin.getline (title,256); 

    cout << name << "'s favourite movie is " << title; 

    return 0; 
} 
+1

然后有人输入超过256个字符的行... – Sjoerd

+0

什么样的麻烦?而且,你为什么要冲洗输入流? –

1

通过使用std::string。没有理由不使用它。

如果在某种模糊的机会下,您有额外的要求,为什么您不能使用std::string,请说明这些要求,因为它们会影响代码。

更新:如果输入的是换行符分隔,使用string s; std::getline(cin, s);

+1

这个问题非常清楚地表明不使用字符串类。 – Mysticial

+2

@Mysticial那么?如果OP指出**为什么**他不能使用字符串类,我们可能会在他的思想中发现错误。也许OP错误地认为'cin >> s'是唯一的输入'std :: string'的方法,在这种情况下,我的答案将解决他的问题。 – Sjoerd

相关问题