簡體   English   中英

在C ++中使用cin

[英]Using cin in C++

我想使用cin,並且將char用作int類型(您這樣稱呼它嗎?),它只顯示所鍵入內容的一個字母。 我怎么能得到整個句子?

由於您使用的是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;
}

使用cin.getline()

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM