View Single Post
Old 02-26-2006, 11:05 AM   #6 (permalink)
destin
Code Monkey
 
Join Date: Mar 2005
Location: NJ
Posts: 40
destin is on a distinguished road
Another approach:
Code:
#include <iostream>
#include <ctime>

using namespace std;

char randomConsonant() {
    srand ((unsigned)time(0));
    char c;
    do {
        c = (rand() % ('a' - 'z' + 1)) + 'a';
    } while ((c == 'a') || (c == 'e') || (c == 'i') || (c == 'o') || (c == 'u'));
    
    return c;
}

char randomVowel() {
    srand ((unsigned)time(0));
    char c;
    do {
        c = (rand() % ('a' - 'z' + 1)) + 'a';
    } while ((c != 'a') && (c != 'e') && (c != 'i') && (c != 'o') && (c != 'u'));
    
    return c;
}

int main() {
    const int SIZE = 9;
    char letterArray[SIZE];
    
    for (int i = 0; i < SIZE; i++) {
        char choice;
        cout << "Would you like a vowel or consonant? <V/C>" << endl;
        cin >> choice;
        
        if (toupper(choice) == 'C') {
            letterArray[i] = randomConsonant();
        } else if (toupper(choice) == 'V') {
            letterArray[i] = randomVowel();
        } else {
            cout << "That is not a valid request!" << endl;
            i--;
        }
    }
    
    for (int i = 0; i < SIZE; i++) {
        cout << endl << letterArray[i];
    }
}
destin is offline   Reply With Quote