//Programmer: Bo Bayles  Date: 12 October 2004
//File: lab8.cpp         Class: CS54 C
//Purpose: This program will prompt the user for a string length, then
//  generate random vowels or consonants based on what the user enters.

#include <string>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
//Include string for the string type; cstdlib for rand and srand; ctime for
//  the time; and iostream for cin and cout.

void AddToString(string &sWord, char cLetterType);
//This function accepts a string by reference and char by value. It calls
//  the RandomLetter function to generate a random letter, and compares the
//  generated letter's type to the type of letter the user entered. If the
//  types match, it concatenates the new letter to the string, then returns
//  nothing.

char RandomLetter();
//This function takes no paramters. It generates a random number between 97
// and 122, assigns the generated integer to a character, then returns the
// character to the calling function.

bool LetterType(char cLetter);
//This takes a char parameter. It compares that char to the list of vowels.
//  If the char is a vowel, it returns a boolean variable indicating the
//  truth or falsehood of the comparison.


int main()
{
  int iStringLength = 0;
  char cLetterType = '0', cContinue = '0';
  string sWord = "";
  //Declare variables for the string length, the type of letter, for the
  //  continuance question, and for the string.

  srand(time(NULL));
  //Seed the randomizer once, outside of the loops.

  cout << endl << "Random String Generator." << endl;

  do
  {
	sWord = "";
	//Initialize the string inside the loop.

    cout << "Please enter the length of the string to be created: ";
    cin >> iStringLength;
    //Get the string length.

    for (int iCount = 0; iCount < iStringLength; iCount++)
    {
      cout << endl << "Should I add a vowel or consonant to the string?"
           << " (v or c): ";
      cin >> cLetterType;

      AddToString(sWord, cLetterType);
    }
    //Loop the same number of times as the user's string length.

    cout << endl << "The string is: " << sWord << endl;

    cout << "Would you like to continue? (y or n): ";
    cin >> cContinue;
    //Ask to continue.
  }
  while (cContinue != 'n' && cContinue != 'N');
  //Loop as long as the user doesn't say no.

  cout << endl << "Exiting..." << endl;
  return 0;
  //Exit normally.
}

void AddToString(string &sWord, char cLetterType)
{
  char cNewLetter = '0';
  bool bLetterType = 0;
  //Delare variables for the letter to be added and for the comparison to
  //  the type of letter needed.

  if (cLetterType == 'v')
  {
    do
    {
      cNewLetter = RandomLetter();

      bLetterType = LetterType(cNewLetter);
    }
    while (bLetterType == false);
  }
  //If the user entered v, call the RandomLetter function. Compare the letter
  //  returned to the type needed by calling the LetterType function. Loop
  //  if the returned value indicates a vowel was not returned.

  else
  {
    do
    {
      cNewLetter = RandomLetter();

      bLetterType = LetterType(cNewLetter);
    }
    while (bLetterType == true);
  }
  //If the user entered c, call the RandomLetter function. Compare the letter
  //  returned to the type needed by calling the LetterType function. Loop
  //  if the returned value indicates a consonant was not returned.

  sWord = sWord + cNewLetter;
  //Add the new letter to the string.

  return;
  //Return to the calling function.
}

char RandomLetter()
{
  char cNewLetter = '0';
  //Declare a variable for the new letter.

  cNewLetter = (97 + (rand() % 26));
  //Generate a new letter in between 97 and 122, the ASCII values for the
  //  lowercase letters. The random number mod 26 can return anything
  //  from 0 to 25. Add 97 to that value to get in between 97 and 122.

  return cNewLetter;
  //Return the generated letter to the calling function.
}

bool LetterType(char cLetter)
{
  bool bLetterType = 0;
  //Declare a variable to hold the truth or falsehood of the comparison.

  if (cLetter == 'a' || cLetter == 'e' || cLetter == 'i' || cLetter == 'o'
       || cLetter == 'u')
  {
    bLetterType = true;
  }
  else
  {
    bLetterType = false;
  }
  //If the char accepted from the calling function is a, e, i, o, or u, return
  //  true. Otherwise return false.

  return bLetterType;
}

