Thread: Name Generator

  1. #1
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65

    Name Generator

    I'm making a name generator (for no reason except practice) except I'm having problems with the string array to store the "syllables"
    Here's my code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    const char filename[]="syllables.txt"; //file to read from
    const int num=105; //number of syllables
    const char syllables[][]={
    "a", "ba", "be", "bi", "bo", "bu", "ca", "ce", "ci", "co", "cu", "da", "de",
    "di", "do", "du", "e", "fa", "fe", "fi", "fo", "fu", "ga", "ge", "gi", "go",
    "gu", "ha", "he", "hi", "ho", "hu", "i", "ja", "je", "ji", "jo", "ju" "ka",
    "ke", "ki", "ko", "ku", "la", "le", "li", "lo", "lu", "ma", "me", "mi", "mo",
    "mu", "na", "ne", "ni", "no", "nu", "o", "pa", "pe", "pi", "po", "pu", "qua",
    "que", "quo", "qui", "ra", "re", "ri", "ro", "ru", "sa", "se", "si", "so",
    "su", "ta", "te", "ti", "to", "tu", "u", "va", "ve", "vi", "vo", "vu", "wa",
    "we", "wi", "wo", "wu", "ya", "ye", "yi", "yo", "yu", "za", "ze", "zi", "zo", "zu"};
    
    int main(void)
    {
    	char *name;
       int namenum=random(2)+2;
    	for(int i=0;i<namenum;i++)
       {
       	name[i]=syllables[random(num)];
       }
       printf("%s",name);
    	return 0;
    }
    I'm probably doing a billion things wrong but that's why I'm still and newb and that's why I'm asking.
    Thanks in advance
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Give the second dimension of your array a value. For example, if they are only ever going to be two letters in length, make it 3.
    Code:
    const char foo[][3] =
    {
        "aa", "ab", "ac", "ad", "ae", ...
    };
    In the future, try posting the errors and warnings you're getting. It aids in the solving of problems. (That is why they're there after all.)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Where's Prelude when you need her?

    Ok, let's go through this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h> // You'll need this later on for srand()
    
    const char filename[]="syllables.txt"; //file to read from
    const int num=105; //number of syllables
    
    const char syllables[][]={ // You have to give sizes for all but the
         // right-most brackets. It would be easier to replace that
        // with const char *syllables[]={
    "a", "ba", "be", "bi", "bo", "bu", "ca", "ce", "ci", "co", "cu", "da", "de",
    "di", "do", "du", "e", "fa", "fe", "fi", "fo", "fu", "ga", "ge", "gi", "go",
    "gu", "ha", "he", "hi", "ho", "hu", "i", "ja", "je", "ji", "jo", "ju" "ka",
    "ke", "ki", "ko", "ku", "la", "le", "li", "lo", "lu", "ma", "me", "mi", "mo",
    "mu", "na", "ne", "ni", "no", "nu", "o", "pa", "pe", "pi", "po", "pu", "qua",
    "que", "quo", "qui", "ra", "re", "ri", "ro", "ru", "sa", "se", "si", "so",
    "su", "ta", "te", "ti", "to", "tu", "u", "va", "ve", "vi", "vo", "vu", "wa",
    "we", "wi", "wo", "wu", "ya", "ye", "yi", "yo", "yu", "za", "ze", "zi", "zo", "zu"};
    
    int main(void)
    {
    	char *name; // You're not reserving memory here, but later on you're
                    // trying to use it like it has memory available. Turn this into an array or
                    // malloc() some memory for it.
    
       int namenum=random(2)+2;  // This is not how you use random(). I'm
                                   // guessing that you want a random number between 2 and 3.  You
                                   // can replace that line with this: int namenum=(rand()%2)+2; but only
                                  // do this after srand() is called. Declare namenum here, but
                                  // Don't assign it a value until further down.
    
      // You should declare i outside the for loop just for portability's sake
      int i;
    
        srand(time(NULL)); // So you get different random numbers each time
    
    
    	for(i=0;i<namenum;i++)
       {
       	name[i]=syllables[random(num)]; // As stated previously, name cannot
               // be used this way until memory has been set aside for it. You also are using
               // random() incorrectly. Replace it with rand()%num
       }
       printf("%s\n",name); // Don't forget the newline
        // You also need to print name differently if you're planning
        // on keeping the same type of for loop. Your for loop
        // isn't creating a string. It's just an array of strings.
    	return 0;
    }
    Last edited by itsme86; 01-07-2005 at 09:15 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    itsme86, many compilers define a random() function that takes an int and returns 0 to int-1, they are using that function correctly. Though since its not portable I wouldn't use it.

    Also not seeding the RNG is a good debug technique.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, let's go through this:

    Quote Originally Posted by itsme86
    Code:
    int main(void)
    {
    	char *name; // You're not reserving memory here, but later on you're
                    // trying to use it like it has memory available. Turn this into an array or
                    // malloc() some memory for it.
    
        srand(time(NULL)); // So you get different random numbers each time
         /* Are we using C99 here? If not, this shouldn't be here. */
    
       int namenum=random(2)+2;  // This is not how you use random(). I'm
                                   // guessing that you want a random number between 2 and 3.  You
                                   // can replace that line with this: int namenum=(rand()%2)+2;
        /* That's not how you use "random", because there is
          no such function.  At least not a standard one... ;) */
    
    
      // You should declare i outside the for loop just for portability's sake
      int i;
      /* What does portability have to do with it? Either you are
        or you aren't using C99. If you are, it doesn't matter. If you aren't, see above. :D */
    
    
    	for(i=0;i<namenum;i++)
       {


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Thantos
    itsme86, many compilers define a random() function that takes an int and returns 0 to int-1, they are using that function correctly. Though since its not portable I wouldn't use it.
    Not to mention that it actually conflicts with other non-standard definitions of random():
    Code:
    NAME
           random,  srandom, initstate, setstate - random number gen-
           erator.
    
    SYNOPSIS
           #include <stdlib.h>
    
           long int random(void);
    
    ...
    
    CONFORMING TO
           BSD 4.3
    Last edited by itsme86; 01-07-2005 at 09:20 PM.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    quzah, I'm not sure what srand(time(NULL)); has to do with C99...

    EDIT: Gah, nevermind. I went back and fixed the placement of srand() before I read your post, so I didn't notice that it was still stuck up there where it shouldn't have been in the code you quoted
    Last edited by itsme86; 01-07-2005 at 09:23 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Bah...just finished poking through and rewriting the code to make it work and I see each item has already been addressed.

    The only thing I will add is that you should become familiar with the strncat() function.

    Also, the line...

    Code:
    const int num=105; //number of syllables
    should probably be replaced with a #define preprocessor directive.
    Last edited by Scribbler; 01-07-2005 at 09:58 PM.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Not to mention that it actually conflicts with other non-standard definitions of random():
    Well you can't really quote one non standard defination and say that another non standard defination isn't correct because it doesn't follow it.

    Besides does anyone follow the BSD standard soley?

  10. #10
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Here's something that might make things a tad bit easier for ya:...be sure to read the comments:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "/home/kleid/Programming/Library/cStandard/kleid.h"
    
    const char syllables[][4]={
    "a",  "ba", "be", "bi", "bo", "bu", "ca", "ce", "ci", "co", "cu", "da", "de",
    "di", "do", "du", "e", 	"fa", "fe", "fi", "fo", "fu", "ga", "ge", "gi", "go",
    "gu", "ha", "he", "hi", "ho", "hu", "i", 	"ja", "je", "ji", "jo", "ju" 	"ka",
    "ke", "ki", "ko", "ku", "la", "le", "li", "lo", "lu", "ma", "me", "mi", "mo",
    "mu", "na", "ne", "ni", "no", "nu", "o", 	"pa", "pe", "pi", "po", "pu", "qua",
    "que","quo","qui","ra", "re", "ri", "ro", "ru", "sa", "se", "si", "so", "su",
    "ta", "te", "ti", "to", "tu", "u", 	"va", "ve", "vi", "vo", "vu", "wa", "we",
    "wi", "wo", "wu", "ya", "ye", "yi", "yo", "yu", "za", "ze", "zi", "zo", "zu"};
    
    int main(void) {
       
       // Create 255 character spots char* name can use.
       char *name = malloc(255 * sizeof(*name));
       // Initialize the current name to..nothing! strlen(name)==0
       strcpy(name, "\0");
       // How large our syllables array is
       int nTotalSyllables = 104;
       // The maximum size we want our name to be
       int nNameMaxSize = 8;
       
       // While the length of the name is lower than our maximum...
       // attach the syllable that we randomly to our cool name!
       while(strlen(name)<nNameMaxSize)
          strcat(name, syllables[randomNumber(0, nTotalSyllables-1)]);
       
       // Print out the great results
       puts(name);
       /*
       Same as:
       printf("%s\n", name);
       */
       return 0;
    }
    And about the randomNumber(int min, int max); function, I left that for you to exercise on :(

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    We don't know what this is:
    Code:
    #include "/home/kleid/Programming/Library/cStandard/kleid.h"
    This:
    Code:
      char *name = malloc(255 * sizeof(*name));
      strcpy(name, "\0");
    can be replaced with:
    Code:
    char name[255] = "";
    Why not #define this and then use it for allocating memory for the name array?
    Code:
    int nNameMaxSize = 8;
    The following is buggy because syllables[x] is more than one character longer. For instance, if nNameMaxSize is 8 and strlen(name) is 7, you will overflow the name array.
    Code:
       while(strlen(name)<nNameMaxSize)
          strcat(name, syllables[randomNumber(0, nTotalSyllables-1)]);
    Lastly, you're not free()'ing your malloc()'d memory.

    Here's a better (read safer) way to do it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define MAXNAMELEN      10
    #define TOTALSYLLABLES 104
    
    const char *syllables[TOTALSYLLABLES] =
    {
      "a",  "ba", "be", "bi", "bo", "bu", "ca", "ce", "ci", "co", "cu", "da", "de",
      "di", "do", "du", "e",  "fa", "fe", "fi", "fo", "fu", "ga", "ge", "gi", "go",
      "gu", "ha", "he", "hi", "ho", "hu", "i", "ja", "je", "ji", "jo", "ju", "ka",
      "ke", "ki", "ko", "ku", "la", "le", "li", "lo", "lu", "ma", "me", "mi", "mo",
      "mu", "na", "ne", "ni", "no", "nu", "o", "pa", "pe", "pi", "po", "pu", "qua",
      "que","quo","qui","ra", "re", "ri", "ro", "ru", "sa", "se", "si", "so", "su",
      "ta", "te", "ti", "to", "tu", "u", "va", "ve", "vi", "vo", "vu", "wa", "we",
      "wi", "wo", "wu", "ya", "ye", "yi", "yo", "yu", "za", "ze", "zi", "zo", "zu"
    };
    
    int main(void)
    {
      char name[MAXNAMELEN+1] = { 0 };
      char *end = name;
      int nsyllables;
    
      srand(time(NULL));
      nsyllables = (rand()%2)+2;
    
      while(nsyllables-- && end-name < MAXNAMELEN)
      {
        strncpy(end, syllables[rand()%TOTALSYLLABLES], MAXNAMELEN-(end-name));
        end += strlen(end);
      }
    
      puts(name);
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define SYLLPERNAME      7
    #define TOTALSYLLABLES 104
    
    const char *syllables[TOTALSYLLABLES] =
    {
      "a",  "ba", "be", "bi", "bo", "bu", "ca", "ce", "ci", "co", "cu", "da", "de",
      "di", "do", "du", "e",  "fa", "fe", "fi", "fo", "fu", "ga", "ge", "gi", "go",
      "gu", "ha", "he", "hi", "ho", "hu", "i", "ja", "je", "ji", "jo", "ju", "ka",
      "ke", "ki", "ko", "ku", "la", "le", "li", "lo", "lu", "ma", "me", "mi", "mo",
      "mu", "na", "ne", "ni", "no", "nu", "o", "pa", "pe", "pi", "po", "pu", "qua",
      "que","quo","qui","ra", "re", "ri", "ro", "ru", "sa", "se", "si", "so", "su",
      "ta", "te", "ti", "to", "tu", "u", "va", "ve", "vi", "vo", "vu", "wa", "we",
      "wi", "wo", "wu", "ya", "ye", "yi", "yo", "yu", "za", "ze", "zi", "zo", "zu"
    };
    
    int main( void )
    {
        const char *name[SYLLPERNAME];
        int x;
    
        srand( (unsigned)time( 0 ) );
    
        /* Build a name. */
        for( x = 0; x < SYLLPERNAME; x++ )
            name[x] = syllables[ rand() % TOTALSYLLABLES ];
    
    
        /* Print a name. */
        for( x = 0; x < SYLLPERNAME; x++ )
            printf( "%s", name[x] );
        printf("\n");
    
        return 0;
    }


    For more fun, add a minimum syllables and do a rand off of that, so all your names aren't the same length.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Only possible problem with your solution itsme is that you are explicitly null terminating. Yes I know that a partually initalized array is suppose to zero out the rest of the array but I'm not willing to trust the compiler to do that.

  14. #14
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Well since others already posted their code, here's how I did it. Similar but a tad different.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    #define SYLNUM 104
    #define MAXSYLLABLES 4
    
    const char *syllables[SYLNUM] = {
    "a", "ba", "be", "bi", "bo", "bu", "ca", "ce", "ci", "co", "cu", "da", "de",
    "di", "do", "du", "e", "fa", "fe", "fi", "fo", "fu", "ga", "ge", "gi", "go",
    "gu", "ha", "he", "hi", "ho", "hu", "i", "ja", "je", "ji", "jo", "ju", "ka",
    "ke", "ki", "ko", "ku", "la", "le", "li", "lo", "lu", "ma", "me", "mi", "mo",
    "mu", "na", "ne", "ni", "no", "nu", "o", "pa", "pe", "pi", "po", "pu", "qua",
    "que", "quo", "qui", "ra", "re", "ri", "ro", "ru", "sa", "se", "si", "so",
    "su", "ta", "te", "ti", "to", "tu", "u", "va", "ve", "vi", "vo", "vu", "wa",
    "we", "wi", "wo", "wu", "ya", "ye", "yi", "yo", "yu", "za", "ze", "zi", "zo", "zu" };
    
    int main(void)
    {
    	char name[(MAXSYLLABLES * 3) + 1] = "";
    	int i;
    	int randSyl;
    	
    	srand( time(NULL) );
    
    	for( i=0 ; i < rand() % (MAXSYLLABLES - 1) + 1 ; i++ )
    	{
    	    randSyl = rand() % ( SYLNUM - 1 );
    	    strncat ( name, syllables[randSyl], strlen(syllables[randSyl]) );
    	}
    	printf( "%s\n", name );
    	
    	return 0;
    }
    I chose to use strncat() hence the reason I initialized name[] from the outset to ensure a null value at the beginning of the string.
    Last edited by Scribbler; 01-08-2005 at 01:20 AM.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Just for fun, it would be a good idea to use a random number with probability so that you can avoid names such as aaaa. I wrote a program like this once for a conlang. The idea was to separate the list of particles into groups sorted by desired frequency, then apply word building rules to the particles grabbed probabilistically from the list.

    The result was much better than the naive approach of just throwing together random sounds.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. X509v3 digital certificate generator
    By CypherHackz in forum C Programming
    Replies: 1
    Last Post: 03-17-2008, 06:18 AM
  2. Replies: 1
    Last Post: 09-04-2007, 05:31 AM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  5. .SFV Generator
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-15-2004, 12:00 PM