Get 5 random strings containing names

This is a discussion on Get 5 random strings containing names within the C Programming forums, part of the General Programming Boards category; Code: I have really stuck in this laboratory, there i need to show on the program 5 random names, but ...

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

    Get 5 random strings containing names

    Code:
    I have really stuck in this laboratory, there i need to show on the program 5 random names, but it only pops up 1 name,
    I Don't know what i m doing wrong here, if someone can help me out would be appreciated, sorry for my bad english ^^, the question is how to make the program get 5 random strings from it.
    
    void SlumpaNamn(char cNamn2[5][10])
    {
    char cNamn[10][10];
    
    strcpy(cNamn[0],"Benny");
    strcpy(cNamn[1],"Glenn");
    strcpy(cNamn[2],"Andreas");
    strcpy(cNamn[3],"Daniel");
    strcpy(cNamn[4],"Patrik");
    strcpy(cNamn[5],"Anders");
    strcpy(cNamn[6],"Gustav");
    strcpy(cNamn[7],"Nisse");
    strcpy(cNamn[8],"........e");
    strcpy(cNamn[9],"Johnny");
    
    int i,a;
    char cTemp[10];
    int iSeed = (int)time(NULL);
    
    srand(iSeed);
    
    
    for(i=0;i<5;i++)
    {
    strcpy(cTemp,cNamn[rand() % 9]);
    
    for(a=0;a<i;a++)
    {
    if (strcmp(cNamn[i],cNamn[a]))
    {
    strcpy(cTemp,cNamn[rand() % 9]);
    a=-1;
    }
    }
    strcpy(cNamn2[i],cTemp);
    printf("%s\n",cNamn2[i]);
    }
    Last edited by kevingarnett; 03-16-2010 at 08:42 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should double-check your expectation about what strcmp does, with what strcmp actually does. (In particular, when is (strcmp(cNamn[i], cNamn[a])) going to be true?)

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Why are you doing all that copying if all you need is to print 5 random names?

    Just do:
    Code:
     
     for(i=0;i<5;i++) 
          printf("%s\n",cNamn[rand()%9]);

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by claudiu View Post
    Why are you doing all that copying if all you need is to print 5 random names?

    Just do:
    Code:
     
     for(i=0;i<5;i++) 
          printf("%s\n",cNamn[rand()%9]);
    I believe OP is using "random" in the sense of "randomly chosen, but distinct".

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well that is a different definition of random every time then, since he is changing the range.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    There seems to be a rash of code being posted with unindented code.

    It's harder for you to work with, and more people will just ignore your post.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matching of names in 2d array of strings...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-25-2009, 10:59 AM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 04:33 AM
  3. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 09:16 AM
  4. an array of names (strings)
    By orion- in forum C++ Programming
    Replies: 7
    Last Post: 09-29-2005, 01:54 PM
  5. malloc with arrays of strings
    By Lib in forum C Programming
    Replies: 2
    Last Post: 08-03-2003, 11:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21