Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #109888

Re: What am I doing wrong here?

From Manfred <noname@invalid.add>
Newsgroups comp.lang.c
Subject Re: What am I doing wrong here?
Date 2017-05-13 17:38 +0200
Organization Aioe.org NNTP Server
Message-ID <of7999$g8p$1@gioia.aioe.org> (permalink)
References <of62cn$4ji$3@dont-email.me> <ennoubFn8ntU1@mid.uni-berlin.de> <of6vp1$m8g$2@dont-email.me> <87o9uwnbcl.fsf@gmail.com> <of7774$f78$2@dont-email.me>

Show all headers | View raw


On 05/13/2017 05:06 PM, DFS wrote:
> On 5/13/2017 9:19 AM, Gareth Owen wrote:
>> DFS <nospam@dfs.com> writes:
<snip>
> 
> Thanks Bart.
> 
> What I intended to do from the start was not just print, but to put 
> random char data into char variable(s), and random int data into int 
> variable(s).  Then I could use the variables to build SQL strings or for 
> other uses.
> 
> This seems to work:
> --------------------------------------------------------------------
> #include <stdio.h>
> #include <time.h>
> 
> int main(int argc, char *argv[])
> {
>    int i;
>    char randName[2];
>    int  randRate=0;
>    char randJob[11];

The size of these buffers needs to be thoroughly considered:
randName: only if all names are guaranteed to be 1-letter names, and 
stay that way, then a 2-char buffer is fine.
randJob: in this example all jobs are max 10-letters long, so an 11-char 
buffer works fine, but they are variable length names already now, what 
if max length may change in future revisions? bart suggested a 100-char 
buffer.

> 
>    char *names[] = {"A","B","C","D","E"};
>    int  rates[]  = {10,20,30,40,50};
>    char *jobs[]  = {"programmer","DBA","analyst","architect","contractor"};
> 
>    int namescount = sizeof(names) / sizeof(names[0]);
>    int ratescount = sizeof(rates) / sizeof(rates[0]);
>    int jobscount  = sizeof(jobs)  / sizeof(jobs [0]);
> 
>    srand(time(NULL));
>    for(i=0;i<atoi(argv[1]);i++)
>    {
>      sprintf(randName, "%s", names[rand() % namescount]);
>      randRate = rates[rand() % ratescount];
>      sprintf(randJob , "%s", jobs [rand() % jobscount]);
> 
>      printf("%s, %d, %s\n",randName, randRate, randJob);
>    }
> 
>    return 0;
> }
> --------------------------------------------------------------------
> 
<snip>
> 
> 
> Do you see ways to improve it?  How would you write it?
> 
> Thanks

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 00:39 -0400
  Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 01:03 -0400
  Re: What am I doing wrong here? jt@toerring.de (Jens Thoms Toerring) - 2017-05-13 06:55 +0000
    Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 08:59 -0400
      Re: What am I doing wrong here? bartc <bc@freeuk.com> - 2017-05-13 14:15 +0100
      Re: What am I doing wrong here? Gareth Owen <gwowen@gmail.com> - 2017-05-13 14:19 +0100
        Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 11:06 -0400
          Re: What am I doing wrong here? Manfred <noname@invalid.add> - 2017-05-13 17:38 +0200
            Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 13:38 -0400
              Re: What am I doing wrong here? bartc <bc@freeuk.com> - 2017-05-13 19:03 +0100
          Re: What am I doing wrong here? bartc <bc@freeuk.com> - 2017-05-13 16:43 +0100
      Re: What am I doing wrong here? Barry Schwarz <schwarzb@dqel.com> - 2017-05-13 07:44 -0700
        Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 11:25 -0400
          Re: What am I doing wrong here? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-13 19:23 +0100
    Re: What am I doing wrong here? Keith Thompson <kst-u@mib.org> - 2017-05-13 13:58 -0700
      Re: What am I doing wrong here? jt@toerring.de (Jens Thoms Toerring) - 2017-05-13 23:43 +0000
        Re: What am I doing wrong here? Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-14 13:43 -0700
  Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-15 20:18 -0400

csiph-web