Converting argv to float

This is a discussion on Converting argv to float within the C++ Programming forums, part of the General Programming Boards category; Hi I have the following minimal non-working example: Code: #include <iostream> #include <stdlib.h> #include <stdio.h> using namespace std; int main(int ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    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.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    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
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting command line char *argv[] to int
    By Winslow in forum C Programming
    Replies: 2
    Last Post: 01-30-2011, 01:41 PM
  2. converting to float
    By jturner38 in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2010, 09:08 PM
  3. Problem with float and argv
    By DonFord81 in forum C Programming
    Replies: 5
    Last Post: 03-04-2009, 01:47 PM
  4. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 04:47 PM
  5. Converting Float to Hexadecimal
    By denizengt in forum C Programming
    Replies: 3
    Last Post: 09-28-2003, 06:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21