Command line argument problems

J

Justin Naidl

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
 
J

John Harrison

Justin Naidl said:
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
 
N

Niels Dekker - no reply address

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top