在字符串到 int 转换中使用 atoi 时出错
error in using atoi in string to int conversion
因此,我尝试使用atoi函数将字符串转换为int,但是我收到一个错误,指出参数类型char与const char*类型的参数不兼容。 这是代码:
void evaluate(const char values[], string& codeMessage, string& result)
{
unsigned int i = 0;
while (i<codeMessage.length())
{
result+= values[atoi(codeMessage[i])];
i++;
}
}
因此,如果调用函数evaluate({a,b,c,d}, "2331", result)
,则结果必须包含"cdda"
。 知道吗,我的代码有什么问题? 谢谢
atoi
需要一个 C 字符串,而不是单个字符。
如果你想在词法上将一个数字转换为等效的整数,为什么不简单地断言它在 '0'
和 '9'
之间,然后减去'0'
?无论区域设置的字符集如何,数字都必须是连续的。
while (i < codeMessage.length()) {
if (codeMessage[i] >= '0' && codeMessage[i] <= '9') {
result += values[codeMessage[i] - '0'];
}
i++;
}
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 为重写std::exception的库生成swig接口时出错
- C++ - 使用 Atoi 时出错
- 在字符串到 int 转换中使用 atoi 时出错