/* Program to play the Name Game */ #include #include //CheckName returns "true" if the name is legal, "false" otherwise bool CheckName(string name); //FindFirstVowel returns the index of the first vowel in name int FindFirstVowel(string name); void main(){ //here we will declare some variables string name, root, indexchar; int i; int NameLength, FirstVowel; //This do..while loop will repeat the game as long as //the user does not enter "done" or "Done" do{ //We start with a do..while loop to make sure the user //inputs a legal name. do{ //First we output a line to the user cout << "Enter a name: "; //Now we get the user's response cin >> name; }while(CheckName(name) == false); FirstVowel = FindFirstVowel(name); //Now that we know where the first vowel is, we can //select the substring that starts there root = name.substr(FirstVowel, NameLength - FirstVowel); //We have all the info we need, so we proceed to output cout << "\n" << name << ", " << name << ", Bo B" << root << endl; cout << "Banana, Fana, Fo F" << root << endl; cout << "Fe, Fi, Mo M" << root << endl; cout << name << "!" << endl; } while(name != "Done" && name != "done"); } //Here is the CheckName function //It returns "true" if the name is "legal," //and returns "false" if the name is not "legal." bool CheckName(string name){ bool retval = true; /* This is the returnvalue...it is initialized to "true" here, and will change if "name" is illegal in any way */ bool VowelFlag = false; /* becomes true if name has a vowel */ int i; /* Used as a counter */ string indexchar; /* Contains the current character */ //We use a loop to check for a vowel for(i = 0; i < name.length(); i++){ indexchar = name.substr(i,1); if(indexchar == "a" || indexchar == "e" || indexchar == "i" || indexchar == "o" || indexchar == "u"){ VowelFlag = true; break; } } if(VowelFlag == false) retval = false; if(name == "chuck" || name == "Chuck") retval = false; return retval; } //Here is the FirstVowel function //It returns the index of the first //vowel in the "name" argument int FindFirstVowel(string name){ //You should fill in the body of this //function so that it calculates and returns //an integer value, just like the "for" loop //in our original program int FirstVowel, i; string indexchar; //Now we use a loop to find the first vowel for(i = 0; i < name.length(); i++){ indexchar = name.substr(i,1); if(indexchar == "a" || indexchar == "e" || indexchar == "i" || indexchar == "o" || indexchar == "u"){ FirstVowel = i; break; } } return FirstVowel; }