string typeVisa (string str);
int main(){
string credit_card;
string type;
getline(cin, credit_card);
type = typeVisa(credit_card);
}
string typeVisa (string str){
int digit1;
int digit2;
digit1 = atoi(str[0]);
digit2 = atoi(str[1]);
// code continues and returns a string
}
“char”类型的参数与“const char *”类型的参数不兼容 这是什么意思^^?
答案 0 :(得分:3)
atoi()
接受一个以空字符结尾的字符串,但您试图将它传递给单个字符。你可以这样做:
char arr[] = {str[0], '\0'};
digit1 = atoi(arr);
arr[0] = str[1];
digit2 = atoi(arr);
...
但是将 char
范围内的 '0'..'9'
转换为等效值的整数的更简单方法是从字符中减去 '0'
,因此:
'0' - '0' = 48 - 48 = 0
'1' - '0' = 49 - 48 = 1
'2' - '0' = 50 - 48 = 2
And so on...
digit1 = str[0] - '0';
digit2 = str[1] - '0';
答案 1 :(得分:-1)
如果你想分别转换每个数字,你可以像下面这样写
//Instead of this:
digit1 = atoi(str[0]);
digit2 = atoi(str[1]);
//use this:
digit1 = (int)str[0] - '0';
digit2 = (int)str[1] - '0';
PS:您可能需要为传递的字符串添加数字验证。