Catching arguments at the command line

Hello everybody.
I'm having a hard time with this one.
Any help is kindly appreciated.

I'm writing a C program in which I want to generate a scrambled alphabet and
store it in char array. When the program is run from the command line, it accepts
an argument(a key), from the user and uses it to randomly shuffle the letters, as
it writes them into the array. The goal is, to use the key to generate a
scrambled alphabet and than share this key with another user, who will run the
program on his computer and be able to generate the same scrambled alphabet with
the shared key.
Anyway. I'm having trouble getting that argument.
I use it to seed the srand

 
srand(argv[1]);


..but when I print it out, I get some bogus numbers instead of what I enter
as an integer argument(ex 131556) when I run the program.

I tried to, instead, create an int variable and assign the value of argv[1]
to it and it didn't work. Compiler complains about type casting and different
sizes of types.

What am I doing wrong?
If there is at least one command-line argument supplied by the user, argv[1] will be a pointer to a string of characters. If you want that to be interpreted as an integer, you need to convert it.

1
2
3
4
5
int number = 0;
if (argc > 0)
{
    number = atoi(argv[1]);
}

he goal is, to use the key to generate a scrambled alphabet and than share this key with another user, who will run the program on his computer and be able to generate the same scrambled alphabet with the shared key.

You are aware that there is not a standard algorithm used for the rand() function, so people building the same code with different compilers might see different sequences generated by rand() for the same seed?

Andy

Linear congruential generator
http://rosettacode.org/wiki/Linear_congruential_generator

(assorted recodings of the BSD and Microsoft rand functions.)
Last edited on
int main(int argc, char **argv)
`argc' holds the number of arguments, meaning that you may access `argv' from `0' to `argc-1'
so Chervil's code is incorrect.
Thank you guys
Andy, thanks for the heads up.
Last edited on
@ne555 Thanks for picking that up.
Topic archived. No new replies allowed.