c++ - 以空格终止

标签 c++

与其这样写代码

while (getline(cin, inputWord))
{
   if (inputWord.empty() || inputWord == " " || inputWord == "  " || inputWord == "   ")return 0;
}

无论存在多少空格,如何让用户终止程序?

最佳答案

制作自己的计算空格的函数:

int countWhiteSpaces(string input)
{
    // You do it :) Hint: a 'for' loop might do
}

然后像这样使用它:

while (getline(cin, inputWord))
{
   if (inputWord.empty() || countWhiteSpaces(inputWord) > 0)
      return 0;
}

关于c++ - 以空格终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21199495/

相关文章:

c++ - 当 template<typename, value...> 时强制特定重载

c++ - 在我的 Fibonacci C++ 代码中找不到段错误的原因

c++ - 在模板化类中分配数组

c++ - C++ #pragma 预处理器可以/应该驻留在函数中吗

C++ 和 Swift 彼此不喜欢

指向类的 C++ 指针

c++ - 为什么 c++ 中没有针对重复的 protobuf 字段的 Set 方法?

c++ - 从流中不可破坏地读取

android - 设置 QTimer 间隔时 Qt 应用程序崩溃

c++ - 自编辑 Makefile