Command line argument types

This is a discussion on Command line argument types within the C++ Programming forums, part of the General Programming Boards category; I have class goto(speed), that takes double as an argument. What i would like to do is to allow user ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    33

    Command line argument types

    I have class goto(speed), that takes double as an argument. What i would like to do is to allow user to set the speed when running the program from command line. The start of program would look like: program_name <file_name> <speed>. So argv[3] would be my speed but the problem is that argv arguments are passed as chars and i need a double. How can i solve this problem?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    strtod().

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    strtod takes a string while argv[] is a pointer. How can i copy the value of argv[3] to some sort of string to pass it to strtod?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    strtod() takes a C string, which is not a std::string. If you checked the prototype for strtod(), you would probably already know this. From the first hit in google:

    Code:
    double strtod ( const char * str, char ** endptr );
    argv[3] is already in a position to be passed directly to strtod().

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    First of all i've read the reference, and second if that is so why does this come up with a segmentation fault?:
    Code:
    double speed;
    
    speed=strtod(argv[3], NULL);
    or even this:
    Code:
    double speed;
    char pend;
    
    speed = strtod(argv[3], &pend)
    Last edited by radnik; 06-30-2008 at 04:53 AM.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Probably because you're rushing to push argv[3] into it when in reality you need to check if argv[3] is even valid by way of argc (ie. if(argc > 3)....).

    Besides, from what you described, you should be using argv[2].

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    You are right. What a stupid mistake. I should have used argv[2]. Thanks for your help MacGyver.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    I would look into boost::lexical_cast, as well. Very handy C++-style stuff.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    33
    Thanks for your suggestion, but i need to have boost libraries for that and i wouldn't like to have any dependencies.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Boost is an excellent library and a must for any C++ programmer if you ask me.
    There's no harm in dependencies. Most of boost are templates anyway, so it gets baked right into your code.
    You could check the boost homepage for any goodies you might find.
    Typical things might include boost::lexical_cast, boost:perators (boost::addable, boost::subtractable, etc), and much more.
    Anyway, it's up to you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 03:25 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM

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