So, I try to use atoi function to convert a string to int, however I got an error saying that argument type char is incompatible with parameter of type const char*. here is the code:
void evaluate(const char values[], string& codeMessage, string& result)
{
unsigned int i = 0;
while (i<codeMessage.length())
{
result+= values[atoi(codeMessage[i])];
i++;
}
}
so, if the function evaluate({a,b,c,d}, "2331", result) is called, the result have to contain "cdda". any idea, what’s wrong w/ my code? thx
atoiexpects a C-string, not a single character.If you want to lexically convert a digit to the equivalent integer, why not simply assert that it’s between
'0'and'9', then subtract'0'? Numbers are required to be consecutive regardless of the locale’s character set.