I\'m writing a very basic command line C++ application that takes arguments on execution.
I just started C++ today, and it seems apparent that you can only take
A char**
in C++ is just that, an pointer to a pointer to a character, thus not convertable to a float
(Or any numeric value for that matter) directly Thus your first example will fail. You can use the intermediary functions atof or strtof (note str
tof, not stof, why your second example fails) to convert these values, assuming you are sure they are actually in float
form.
You are just converting a char *
variable to float
by typecasting. It will typecast value of the pointer to float, not the string to float.
You need to use functions such as atof
, strtof
to convert string to float. Or you can write your own function to to convert string to float.
Using this to convert your input to float number,
double f1, f2;
if (argc == 2)
{
f1 = atof (argv[0]);
f2 = atof (argv[1]);
}