检查字符串字符和长度

check string character and length

本文关键字:字符 字符串 检查      更新时间:2023-10-16

此代码假设只接受数字,并且数字必须正好是 12 个数字。前者有效,但后者无效。用户输入 13 个数字,但不显示任何错误。

 std::string line;
    double d;
    while (std::getline(std::cin, line))
    {
        std::stringstream ss(line);
        if (ss >> d || line.size() == 12)
        {
            if (ss.eof())
            {   // Success
                break;
            }
        }
        std::cout << "Error!" << std::endl;
    }

请有人可以帮助我解决这个问题吗?提前谢谢。

尝试代替or

if (ss >> d && line.size() == 12)