3

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.

scbeacham
  • 279
  • 2
  • 4
  • 12
  • `argv` is a `char**`, not `char*`... – David G Nov 28 '13 at 18:25
  • argv is a pointer to a list of character pointers (words) separated by white space characters (the command line variables). try *argv for the first variable (you need to check it exists first) – Joel Nov 28 '13 at 18:27

1 Answers1

4

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.

James Smith
  • 1,072
  • 1
  • 11
  • 14
  • 1
    `argc` is almost always at least 1, `argv[0]` is this case is almost always the program path, convert it to float* makes no sense. – yngccc Nov 28 '13 at 18:30
  • Hey James, I updated my code to reflect your solution, but I was then given: "error: cannot convert ‘double’ to ‘float*’ in initialization" Any other suggestions? (The rest of my code is literally exactly as it is displayed above). – scbeacham Nov 29 '13 at 20:43
  • Nevermind! I just changed float *feet to float feet and the code worked perfectly! Thank you! – scbeacham Nov 29 '13 at 20:49
  • 1
    Great.. I missed the *float. I was coding if from memory. I will update the solution incase someone needs the answer in the future. – James Smith Nov 29 '13 at 21:20