【发布时间】:2021-09-06 14:05:57
【问题描述】:
我正在尝试将存储在罗马数字字符串 s 中的 III 转换为 3。这是一个代码 sn-p:
int i = 0;
int num = 0;
while (i < s.size()){
if (strcmp(s[i], "I") == 0){
num = num + 1;
i = i + 1;
}
else{
continue;
}
}
return num;
我在使用 strcmp() 函数时遇到问题。怎样才能成功使用?
这是错误:
Line 18: Char 17: error: no matching function for call to 'strcmp'
if (strcmp(s[i], "I") == 0){
^~~~~~
/usr/include/string.h:137:12: note: candidate function not viable: no known conversion
from '__gnu_cxx::__alloc_traits<std::allocator<char>,
char>::value_type' (aka 'char') to 'const char *' for 1st argument;
take the address of the argument with &
extern int strcmp (const char *__s1, const char *__s2)
^
【问题讨论】:
-
使用
s.size()表示这不是C。标记您正在使用的语言。顺便说一句,这:IIIIIIIIIIIIIIIIIIIII不是一个有效的罗马数字,但你的程序会像处理它一样处理它。 -
s[i] == 'I'...您正在比较字符,而不是 c 字符串。