strtol
is used to convert a "string" into long
, not a single char. You just need choice = option - '0'
to get the value. But you don't actually need to convert because you can directly switch on the char value
switch (option)
{
case '1':
// ...
break;
case '2':
// ...
break;
}
If you really want to call strtol
then you must make a string
char[2] str;
str[0] = option;
str[1] = 0; // null terminator
choice = strtol(str, &endptr, 10);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…