Click here to Skip to main content
12,948,021 members (86,078 online)
Rate this:
Please Sign up or sign in to vote.
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 13-Aug-09 0:30am
Updated 6-May-10 20:42pm
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 :-)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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!

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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;
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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.
Keith Barrow 14-May-11 10:05am
Come back Christian, this is why we need you!
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

if(i%2==1) can be simply written as if(i&1)

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,344
CHill60 3,275
Maciej Los 2,628
Jochen Arndt 1,935
ppolymorphe 1,795

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 7 May 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100