Thread: Problem with either rand() or loops....not sure

  1. #1
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168

    Problem with either rand() or loops....not sure

    My aim is to print out a grid of random characters, either a G, a W or an A. Here is my code (obviously with int main() etc):

    Code:
        srand(time(0));
        
        char terrain[5][5];
        
        for (int i = 0;i < 5; i++)
        {
            for (int j = 0;j < 5; j++)
            {
                int temp = (rand() % 2) + 1;
    
                switch (temp)
                {
                       case 1:
                            terrain[i][j] = 'G';
                            break;
                       case 2:
                            terrain[i][i] = 'W';
                            break;
                       default:
                            terrain[i][j] = 'A';
                            break;
                }
            }
        }
        
        for (int i = 0;i < 5; i++)
        {
            cout << "\n";
            for (int j = 0;j < 5; j++)
            {
                cout << terrain[i][j] << "\t";
            }
        }
    But the problem is a get some characters i want, plus i get some unexpected characters, i've played with the rand() function but i cant seem to figure out what im doing wrong, some help would be appreciated, thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Surely you're not expecting any 'A's with that code?

    > terrain[i][i] = 'W';
    Oh, and check the subscripts here - seems a little lacking on the J front
    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.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    int temp = (rand() % 2) + 1;

    temp can only ever be 1 or 2, so I don't think you'll ever get any A's.

    Could you provide a sample output?

  4. #4
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    ah a simple friggin typo! I only put the 'A' in there to see if it was the rand() causing my problems and generating numbers i didnt want, thanks for that

    new lesson learnt - proof-read code first :P

    **edit** i only wanted temp to be a 1 or 2

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    To prevent undefined behaviour from a typo, you could've intialised your variable.

  6. #6
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    what do you mean? i initialised it in the loop

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I mean initializing it with pre-determined values. For example, setting each char in the array equal to '_'. Then when you try filling the array with other values latter on, if you see any underscores in your output, it's likely that you haven't successfully written to those parts of the array.

  8. #8
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    yeah that's what the 'A' was for, if i wasnt getting the values i wanted then it would print 'A', but i see what you mean, ill remember to try that one out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM