相关疑难解决方法(0)

如何检查给定的c ++字符串或char*是否只包含数字?

或者从另一个方向找到第一个非数字字符.

相同的函数是否适用于字符串和char*?

c++ string pattern-matching

45
推荐指数
4
解决办法
8万
查看次数

只接受信件

这应该只接受字母,但它还不正确:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    std::string line;
    double d;

    while (std::getline(std::cin, line))
    {
        std::stringstream ss(line);
        if (ss >> d == false && line != "") //false because can convert to double
        {
            std::cout << "its characters!" << std::endl;
            break;
        }
        std::cout << "Error!" << std::endl;
    }
    return 0; 
}
Run Code Online (Sandbox Code Playgroud)



这是输出:

567
Error!

Error!
678fgh
Error!
567fgh678
Error!
fhg687
its characters!
Press any key to continue . . .
Run Code Online (Sandbox Code Playgroud)

fhg687 应该输出错误,因为字符串中的数字.

接受的输出应仅包含字母,例如ghggjh …

c++ std

3
推荐指数
2
解决办法
2万
查看次数

标签 统计

c++ ×2

pattern-matching ×1

std ×1

string ×1