Understanding Error "error: cannot convert ‘char**’ to ‘const char*’ for argument ‘1’ to ‘double atof(const char*)’"

StackOverflow https://stackoverflow.com/questions/20272648

Domanda

So I'm working on this assignment for my class and book is REALLY unclear about how to use the argc, argv, and atof() items (which are all new to me), so I am trying to use them but I am getting this error, and I'm not quite sure how to correct it:

This is my Error:

error: cannot convert ‘char**’ to ‘const char*’ for argument ‘1’ to ‘double atof(const char*)’

This is my Code:

int main(int argc, char** argv)
{
   //Code removed

   //Code removed

   float *feet = atof(argv); // <-- it says the error is with this line

   //Code removed

   return 0;
}

float convertFeet(float feet)
{
   float meters = feet * .3048;

   return meters;
}

The "purpose" of this program is to convert given feet into a float, and then display those (while also converting them to meters). I already know the convertFeet() function works as I tested it before adding the atof(), argc, & argv portions of the program. If anyone could help me understand this error better and the solution that would be very helpful!

Thanks again,

-Stephen

P.S. This is for sure a compile error as my compiler told me. I just didn't post that portion of the error as I considered that portion of the code to be superfluous.

È stato utile?

Soluzione

char** argv can contain multiple "strings". (I know C/C++ doesn't technically have a string type, but I hope you get conceptually what i mean). argv is an array of char * (strings) including the name of the executable and all parameters;

For example: myfile.exe param1 param 2

would yield

argv[0] = myfile.exe
argv[1] = param1
argv[2] = param2

and argc = 3

You can do this a couple of different ways, but the easiest:

float feet = atof(argv[1]);

You should do some error checking to make sure that argc is at least 2 or that will blow up on you. There could also be more than one parameter argv[2], 3 etc.. (If argc == 1 there is no parameters supplied)

Hope that helps.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top