On Sat, 8 Mar 2008 12:21:06 -0600 (CST), Scooter <slbentley@[EMAIL PROTECTED]
>
wrote:
>I'm trying to write a simple program to generate a random string. I
>have what seems to work, although I'm guessing there are better ways
>to do it. But my overall goal is to generate a random character, then
>add a second random character to it, then another, etc. until the
>string grows to a max of 10 characters. I'm testing a word wheel on a
>website but have to use 'C' and it is not my forte'. Anyway, here's
>what I have:
>
>int myRand,myLoop;
>char myChar[1];
What do you think the point of a one element array is?
>char myLoc[11];
>
>srand((unsigned int)time(0)); //Seed number for rand()
>
>for (myLoop =3D 0; myLoop < 10; myLoop++) {
> myRand =3D rand() % 25 + 65;
> myChar[0] =3D (char)myRand;
> myLoc[myLoop] =3D myChar[0] ;
> printf("%s", myLoc);
>}
>
>
>
>I would expect the output to look something like:
>V
>VD
>VDY
>VDYW
>VDYWQ
>VDYWQA
>VDYWQAE
>VDYWQAEM
>VDYWQAEMQ
>VDYWQAEMQJ
>
>But instead it looks more like:
>V
>VD
Since there is no \n in the above code, how did you second output end
up on a new line? Perhaps you would care to show us the actual coded.
>VDY
>VDYW=A6=0EB
Nowhere do we see where myLoc is initialized so how does your string
ever get terminated with a '\0'? It appears that there were a few \0s
at the beginning of the array by happenstance but the W replaced one
and you are getting the garbage that is in myLoc[4] and following
elements.
>VDYWQ=0EB
>VDYWQAB
>VDYWQAE
>VDYWQAEM=FC=0EB
>VDYWQAEMQ=0EB
>VDYWQAEMQJB
Remove del for email
--
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
-- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line. Sorry.


|