-
Converting argv to float
Hi
I have the following minimal non-working example:
Code:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main(int argc, char* argv[])
{
double b = strtod(argv[1], NULL);
return 0;
}
I am trying to convert argv[1] to a double, but my example crashes when I run it. I don't really understand why. I am using code::blocks, does it have something to do with that?
Best,
Niles.
-
Check argc if enough parameters where passed to your executabe. Maybe argv[1] does not even exist? Also, if you don't have to use strtod, atof is a much easier function to handle.
The C++ way would probably be to write argv[1] to a stringstream and then read a double from it. But that's probably way over your head for now, check argc first :)