在C ++中使用cin

时间:2011-06-24 01:55:52

标签: c++ char cin

我想使用cin,我使用char作为int类型(你这样称呼它吗?)它只显示了一个键入的字母。我怎么能得到整句话?

2 个答案:

答案 0 :(得分:5)

既然您正在使用c ++,为什么不使用std::string呢?这样的事情应该做你想要的:

#include <iostream>
#include <string>

int main()
{
  using namespace std;
  string sentence;
  cout << "Enter a sentence -> ";
  getline(cin, sentence);
  cout << "You entered: " << sentence << endl;
}

答案 1 :(得分:2)

使用cin.getline()

char name[256];
cout << "What is your name?\n>";
cin.getline(name, 256);

cout << "Your name is " << name;