不确定为什么我不能从字符串转换为 int?

Not sure why I can't convert from string to int?

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 *”类型的参数不兼容 这是什么意思^^?

如果你想分别转换每个数字,你可以像下面这样写

//Instead of this:
digit1 = atoi(str[0]);
digit2 = atoi(str[1]);

//use this:
digit1 = (int)str[0] - '0';
digit2 = (int)str[1] - '0';

PS:您可能需要为传递的字符串添加验证是数字。

atoi() 接受一个以 null 结尾的字符串,但您试图向它传递一个字符。您可以这样做:

char arr[] = {str[0], '[=10=]'};
digit1 = atoi(arr);

arr[0] = str[1];
digit2 = atoi(arr);

...

但是将 '0'..'9' 范围内的 char 转换为等值整数的更简单方法是从字符中减去 '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';