Thread: Audit this simple code?

  1. #1
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44

    Audit this simple code?

    Mind just checking this code out for me? It should compile fine and generally be error free, although I'm realeasing it on my website, and I dont want there to be too many nastys in it. If there is a better way of doing things, please do let me know. It's a simple random number generator.

    Code is here: http://www.jwread.com/projects/randgen.cpp

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    #include <stdlib.h>
    #include <time.h>
    No need to include stdlib.h, you already included cstdlib.

    Also time.h should be ctime

    2. Both exit paths of main return 0.
    There is EXIT_SUCCESS and EXIT_FAILURE, so I would like to see EXIT_FAILURE whenever the program fails to perform it's intended function.

    > cout << "Correct useage: [numbers]\n";
    When outputting error or help information, it's normal to use cerr

    > count = 1;
    Given the prior test of argc, I don't think this line can ever be reached.

    > if (argv[1])
    Again, given the previous argc test, this test is trivially true.
    argv[argc] is always NULL and all the argv before then will be non-NULL.

    > count = atoi(argv[1]);
    A better function is strtod(), which returns more error information, for example if there is overflow.

    > for(int i = 0; i <= count; i++)
    This executes one more time that you expect.
    If you ask for say 2 numbers, you'll get 3

    > printf("%d\n", rand()%10);
    Using printf() in a C++ program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 11-23-2005, 08:53 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 11:34 AM