开发者

Using cin in C++

I'd like to use cin and I used char for the int type (do you c开发者_运维技巧all it like that?) and it just shows one letter of what typed. How can I get the whole sentence?


Since you're using c++ why not use std::string instead? Something like this should do what you're looking for:

#include <iostream>
#include <string>

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


use cin.getline()

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜