Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ GimmeCode Homework
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 1:30am
Edited 6-May-10 21:42pm
CPallini326.3K
v2
Comments
Sandeep Mewara at 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
good
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()
{
   const char vowels[] = {'a', 'e', 'i', 'o', 'u'};
   // Use rand() to get a number between 0 and RAND_MAX. Dividing
   // by RAND_MAX+1 gives a number n such that 0 <= n < 1, so
   // multiplying by the number of characters in vowels gives a random
   // index into vowels
   const int index = (int)((double)rand()/(RAND_MAX+1) * sizeof(vowels));
   return vowels[index];
}


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;
   name += (char)toupper(RandomConsonant());
   for(int i=1;i<desired-name-length;++i)
   {
      if (i%2==1)
         name += RandomVowel();
      else
         name += RandomConsonant();
   }


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

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

Solution 3

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

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

  Print Answers RSS
0 OriginalGriff 505
1 Maciej Los 325
2 Richard MacCutchan 265
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 7 May 2010
Copyright © CodeProject, 1999-2014
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