Thread: Converting argv to float

  1. #1
    Registered User
    Join Date
    Aug 2009

    Converting argv to float


    I have the following minimal non-working example:

    #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?


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

    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, 02:41 PM
  2. converting to float
    By jturner38 in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2010, 10:08 PM
  3. Problem with float and argv
    By DonFord81 in forum C Programming
    Replies: 5
    Last Post: 03-04-2009, 02: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