Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
4.65/5 (8 votes)
See more:
I am looking for some help to create a random name generator with ASCII. I am new to C++ and really and not 100% sure how to attack this. "First and last names should be randomly generated and must begin with a capital letter. Additionally, the first letter should be a consonant. The remainder of the name should alternate between vowels and consonants, where every even numbered letter is a vowel and every odd numbered letter is a consonant." Any help would be GREAT!!.
Posted
Updated 6-May-10 20:42pm
v2
Comments
Sandeep Mewara 7-May-10 2:47am    
How come 5 months old question suddenly pops here! Looks like already a lot has happened in it :-)

The way I'd approach it is like this:

1. Define functions that return random voewls and random consonants - here's a random vowel function:

char RandomVowel()<br />{<br />   const char vowels[] = {'a', 'e', 'i', 'o', 'u'};<br />   // Use rand() to get a number between 0 and RAND_MAX. Dividing<br />   // by RAND_MAX+1 gives a number n such that 0 <= n < 1, so<br />   // multiplying by the number of characters in vowels gives a random<br />   // index into vowels<br />   const int index = (int)((double)rand()/(RAND_MAX+1) * sizeof(vowels));<br />   return vowels[index];<br />}


2. Build up a string using alternating calls to the random vowel and consonant functions - note that I've remembered about the upper case first character!

std::string name;<br />   name += (char)toupper(RandomConsonant());<br />   for(int i=1;i<desired-name-length;++i)<br />   {<br />      if (i%2==1)<br />         name += RandomVowel();<br />      else<br />         name += RandomConsonant();<br />   }


Obviously the desired name length should be chosen at random as well!

 
Share this answer
 
Here's my try:

#include <string>
using namespace std;

int main(int argc, char* argv[])
{
	string name;
	static const int namelen = 15;
	
	static const char* letters[2] = { "bcdfghjklmnpqrstvwxyz", "aeiouy" };
	static const letterlen[2] = { strlen(letters[0]), strlen(letters[1]) };
	
	for (int i=0; i<namelen; i++)
		name += letters[i%2][rand()%letterlen[i%2]];
	name[0] = toupper(name[0]);

	return 0;
}
 
Share this answer
 
v2
I am sorry that everyone is doing your homework for you. You won't learn anything by copying and pasting the code you were given, without first reading through it and understanding it. You should also remember that your teacher has access to google, if they have any sense, they will google their original question, as pasted here by you, and will fail anyone who turns in the code presented here, verbatim. What you should do, is try to learn from the code you've been given, then try to write your own code, and perhaps ask *specific* questions if you get stuck while trying to *do* your homework, as opposed to asking us to do it for you.
 
Share this answer
 
Comments
Keith Barrow 14-May-11 10:05am    
Come back Christian, this is why we need you!
if(i%2==1) can be simply written as if(i&1)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900