开发者

Using cin in C++

开发者 https://www.devze.com 2023-03-15 14:20 出处:网络
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

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

精彩评论

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