Command line argument problems

Discussion in 'C++' started by Justin Naidl, Sep 17, 2004.

  1. Justin Naidl

    Justin Naidl Guest

    I'm having trouble getting some arguments to work from the command line. A
    program I have excepts three arguments: "pgm argA argB argC. The first two
    arguments are filenames the last is an integer. I am having no trouble with
    the first two however the third is giving me some trouble. If I enter the
    value '3' as the third argument and cout it it displays '3' on the screen.
    However, I need to use it as an integer and if I try to cast it as an
    integer I get the number '-1073743214'. Does anybody no how to fix this or
    what the problem might be? I've tried a number of different castings. I
    understand that the arguments value is a 'char *' my main looks like this.
    int main(int argc, char* argv[])
    {
    stuff
    }

    I pass it to a function
    cksum(argv[2],(int)(argv[3]));

    I get no errors just the tainted value. Any suggestions?

    Thanks
    ~Justin
     
    Justin Naidl, Sep 17, 2004
    #1
    1. Advertising

  2. "Justin Naidl" <> wrote in message
    news:peJ2d.1673$...
    > I'm having trouble getting some arguments to work from the command line.
    > A
    > program I have excepts three arguments: "pgm argA argB argC. The first
    > two
    > arguments are filenames the last is an integer. I am having no trouble
    > with
    > the first two however the third is giving me some trouble. If I enter the
    > value '3' as the third argument and cout it it displays '3' on the screen.
    > However, I need to use it as an integer and if I try to cast it as an
    > integer I get the number '-1073743214'. Does anybody no how to fix this
    > or
    > what the problem might be? I've tried a number of different castings. I
    > understand that the arguments value is a 'char *' my main looks like
    > this.
    > int main(int argc, char* argv[])
    > {
    > stuff
    > }
    >
    > I pass it to a function
    > cksum(argv[2],(int)(argv[3]));
    >
    > I get no errors just the tainted value. Any suggestions?
    >
    > Thanks
    > ~Justin
    >


    You misunderstand what a cast is. What you need is a conversion not a cast.
    Simplest conversion function to use is atoi, declared in <stdlib.h>

    cksum(argv[2],atoi(argv[3]));

    atoi does no error checking however. strtol is better is you want to check
    for errors.

    john
     
    John Harrison, Sep 17, 2004
    #2
    1. Advertising

  3. Justin Naidl wrote:
    [...]
    > If I enter the value '3' as the third argument and cout it it
    > displays '3' on the screen. However, I need to use it as an integer
    > and if I try to cast it as an integer I get the number '-1073743214'.
    > Does anybody no how to fix this or what the problem might be? I've
    > tried a number of different castings.

    [...]
    >
    > I pass it to a function
    > cksum(argv[2],(int)(argv[3]));
    >
    > I get no errors just the tainted value. Any suggestions?


    John Harrison replied:
    >
    > You misunderstand what a cast is. What you need is a conversion not a cast.
    > Simplest conversion function to use is atoi, declared in <stdlib.h>
    >
    > cksum(argv[2],atoi(argv[3]));
    >
    > atoi does no error checking however. strtol is better is you want to check
    > for errors.


    I'm not so sure if the OP misunderstands the word "cast"... Boost's
    lexical_cast should do the job as well:

    #include <boost/lexical_cast.hpp> // From www.boost.org

    try
    {
    cksum(argv[2], boost::lexical_cast<int>(argv[3]));
    }
    catch(const boost::bad_lexical_cast &)
    {
    std::cout
    << "Lexical cast from \"" << argv[3] << "\" to int failed"
    << std::endl;
    }

    Regards,

    Niels Dekker
    www.xs4all.nl/~nd/dekkerware
     
    Niels Dekker - no reply address, Sep 18, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fong
    Replies:
    3
    Views:
    1,166
  2. Thomas Kellerer
    Replies:
    5
    Views:
    34,590
    Nomak
    Apr 13, 2005
  3. nic977

    reading in command line argument

    nic977, Sep 12, 2003, in forum: C Programming
    Replies:
    13
    Views:
    610
    Dave Thompson
    Sep 22, 2003
  4. Steven Knight
    Replies:
    0
    Views:
    448
    Steven Knight
    Nov 7, 2003
  5. Joe
    Replies:
    8
    Views:
    434
Loading...

Share This Page