Thread: My function won't compile :(. Any help?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    My function won't compile :(. Any help?

    Hi all,
    I'm still kind of new to writing functions, and this is one i wrote that is supposed to generate a random numerical code of length "d", with maximum value for each digit of "h" and where "c" is the code.
    For example if d=4, h=3, then c could equal 1232, or 1132, or 2333 etc.

    Here's my function:

    int pickCode(int d, int c, int h);
    int pickCode(int d, int c, int h){
    using namespace std;
    int counter = 0;
    int digit;
    c = 0;
    while (counter<d) {
    rand( digit % d + 1);
    c = c*10 + digit;
    counter = counter + 1;
    }
    return c;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Seems you are missing a particular detail of rand() that might be helpful.

    rand [C++ Reference]
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Seems like you missed this in your rush to post
    << !! Posting Code? Read this First !! >>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM