1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include<iostream>
using std::cout;
using std::cin;
#include<cstdlib>
#include<ctime>
void print_random_welcome_phrase();
void print_random_farewell();
char generate_random_consonant(char);
char generate_random_vowel(char);
int number_right(char, char, char, char, char, char);
char get_y_or_n();
int main()
{
srand(time(0));
char consonant1, vowel, consonant2, consonant1_guess, vowel_guess, consonant2_guess, again;
int num_guesses, correct_letters;
const int MAX_GUESSES_ALLOWED = 10;
system("cls");
print_random_welcome_phrase();
do
{
num_guesses = 0;
generate_random_consonant( consonant1 );
generate_random_vowel( vowel );
generate_random_consonant( consonant2 );
cout << consonant1 << vowel << consonant2; // for testing purposes i added this line
do
{
cout << "\n\nTry to guess the three letter \"word\" I'm thinking of.\nThe word is in the form:"
<< " consonant vowel consonant.\n";
cin >> consonant1_guess >> vowel_guess >> consonant2_guess;
correct_letters = number_right( consonant1, vowel, consonant2, consonant1_guess, vowel_guess, consonant2_guess );
++num_guesses;
}while ( correct_letters != 3 && num_guesses < MAX_GUESSES_ALLOWED );
if ( correct_letters == 3 )
cout << "You guessed correctly in only " << num_guesses << " tries!.\n";
else
cout << "You ran out of guesses. The correct answer was " << consonant1 << vowel << consonant2 << ".\n";
cout << "Try again? (y or n) ";
again = get_y_or_n();
system("cls");
} while ( again == 'y' );
print_random_farewell();
system("pause");
return 0;
}
void print_random_welcome_phrase(){
int welc_phrase;
welc_phrase = rand() % 5;
switch(welc_phrase){
case 0:
cout << "Hello.\n";
break;
case 1:
cout << "Welcome.\n";
break;
case 2:
cout << "Good day.\n";
break;
case 3:
cout << "Oh Hai.\n";
break;
case 4:
cout << "You rang?.\n";
break;}
return;}
char generate_random_consonant( char consonant ){
consonant = rand() % 26;
do{
consonant = 'a' + consonant;
}while (consonant != 'b' && consonant != 'c' && consonant != 'd' && consonant != 'f'
&& consonant != 'g' && consonant != 'h' && consonant != 'j' && consonant != 'k'
&& consonant != 'l' && consonant != 'm' && consonant != 'n' && consonant != 'p'
&& consonant != 'q' && consonant != 'r' && consonant != 's' && consonant != 't'
&& consonant != 'v' && consonant != 'w' && consonant != 'x' && consonant != 'y'
&& consonant != 'z');// ensures that returned value is a consonant
cout << consonant;
return consonant;}
char generate_random_vowel( char vowel ){
vowel = 'a' + rand() % 26;
do{
vowel = 'a' + vowel;
}while (vowel != 'a' && vowel != 'e' && vowel != 'i' && vowel != 'o' && vowel != 'u');//ensures returned value is a vowel
cout << vowel;
vowel = vowel;
return vowel;}
// theoretically this should compare the values from the function calls to the user input values
int number_right(char letter1, char letter2, char letter3, char guess1, char guess2, char guess3){
int correct=0;
cout << letter1 << letter2 << letter3 << guess1 << guess2 << guess3; // this is only here for my current debugging purposes, so i can see what is being compared
if (letter1 == guess1)
correct = correct + 1;
if (letter2 == guess2)
correct = correct + 1;
if (letter3 == guess3)
correct = correct + 1;
cout << "You have "<<correct<<" letters correct.";
return (correct);}
// simply gets a y or n
char get_y_or_n(){
char y_or_n;
do{
cout << "Would you like to try another one?";
cin >> y_or_n;
tolower (y_or_n);// this doesn't work either, but thats not my main problem right now
}while (y_or_n != 'y' && y_or_n != 'n');
return y_or_n;}
void print_random_farewell(){
int bye;
bye = rand() % 5;
switch(bye){
case 0:
cout << "Bye.\n";
break;
case 1:
cout << "Good-bye.\n";
break;
case 2:
cout << "Later.\n";
break;
case 3:
cout << "Have a nice day.\n";
break;
case 4:
cout << "No! Don't leave me!\n";
break;}
return;}
|