I have a variable where the user inputs one number. That variable is of type int
because that's the return value of fgetc(stdin)
and getchar()
. In this case, I'm using fgetc(stdin)
. After the user inputs the number, I would like to convert it to an integer with strtol()
, however, I get a warning about an incompatible integer to pointer conversion because of strtol()
's first argument being a const char *
. Here is the code:
int option;
char *endptr;
int8_t choice;
printf("=========================================Login or Create Account=========================================\n\n");
while(1) {
printf("Welcome to the Bank management program! Would you like to 1. Create Account or 2. Login?\n>>> ");
fflush(stdout);
option = fgetc(stdin);
choice = strtol(option, &endptr, 10);
Does anyone know how to get around this?
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);