首页 > 编程 > C++ > 正文

C++实现从输入中读取字符串

2020-01-26 14:35:22
字体:
来源:转载
供稿:网友

你可以用这种方式读取一个单独的以空格结束的词:

#include<iostream>#include<string>using namespace std;int main(){  cout << "Please enter a word:/n";  string s;  cin>>s;  cout << "You entered " << s << '/n';}

注意,这里没有显式的内存管理,也没有可能导致溢出的固定大小的缓冲区。

如果你确实想得到一行而不是一个单独的词,可以这样做:

#include<iostream>#include<string>using namespace std;int main(){  cout << "Please enter a line:/n";  string s;  getline(cin,s);  cout << "You entered " << s << '/n';}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表