运行此命令时,我希望不能以cin
哪种格式说明符输入任何空格?
他们的一切<string>
让我做到了吗?
#include <iostream>
#include <string>
using namespace std;
int main(){
string yy;
cin >> string;
/*when this runs I want to not be able to enter any spaces in `cin`
what format specifier can I use for this? is their anything in `<string>` that lets me do this?
*/
return 0;
}
您可以使用std::noskipws
禁用在格式化输入之前自动跳过空格的操作:
if (std::cin >> std::noskipws >> yy) {
std::cout << "read '" << yy << "'\n";
}
如果用户在字符串之前输入空格,则会出现错误,因为无法成功读取任何单词。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句