【发布时间】:2017-12-16 16:20:23
【问题描述】:
我正在测试 CodeBlocks 中程序的字符串输出。这是代码:
#include <iostream>
#include <string>
using namespace std;
int main(){
string entry = "";
while(entry!="x"){
cout<<"Enter: ";
cin>>entry;
cout<<entry.substr(0,1)<<endl;
}
return 0;
}
但是打印结果好像它被空格和 cout 字符串 "Enter" 以错误的顺序分隔,就好像它被窃听一样。可能是什么原因,我该如何解决这种情况?
Output:
Enter: P q r
P
Enter: q
Enter: r
Enter:
【问题讨论】:
-
>>运算符提取空格分隔的内容。有好书清单here. -
这就是流式输入的正常工作方式。您可能想在 std::string 中使用
getline。
标签: c++ string output codeblocks