当前位置:首页 > 通信资讯 > 正文

c语言读取输入字符串(c++读入字符串并输出)

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

?
1 2 3 4 5 6 7 8 9 #include<iostream> #include<string> using namespace std; int main(){ cout << "Please enter a word:\n"; string s; cin>>s; cout << "You entered " << s << '\n'; }

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

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

?
1 2 3 4 5 6 7 8 9 #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'; }
如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。