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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
//Header includes...//
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
//Usings...//
using namespace std;
//Prototype functions...//
void animalGuess();
void WinGame();
string loseGame();
//Named constants...//
const string lion;
const string animal = lion;
//Start of main program...//
int main()
{
//Declared variables...//
string playerName;
char menuChoice;
string guess;
ifstream inFile;
ofstream outFile;
bool gameContinue = true;
//Start of Main Menu display//
cout << "Welcome to the Animal Guessing Game!\n" << endl;
cout << endl;
cout << "You will have a total of 5 attempts to guess the animal.\n "
<< "Enter your guess after each hint." << endl;
cout << endl;
cout << "For the duration of the game, please ensure\n "
<< "your 'CAPS LOCK' is off and you use all lower case letters." << endl;
cout << endl;
inFile.open("player.txt");
inFile >> playerName;
cout << "Are you player, " << playerName << "? Enter: y if so,\n "
<< "otherwise, enter: n ...to register a new name." << endl;
cout << endl;
inFile.close();
cin >> menuChoice;
cout << endl;
//Nested Menu Choice for invalid menu entry//
while (menuChoice != 'y' && menuChoice != 'n')
{
cout << "Invalid choice. Please enter either a 'y' or an 'n'." << endl;
cin >> menuChoice;
}//endWhile//
//Sub Menu Choice: Returning player//
if (menuChoice == 'y')
{
cout << "Welcome back, " << playerName << "!"
<< "I hope your memory is good! Game starts now!" << endl;
animalGuess();
}//endIf
//Sub Menu Choice: Get new player's name//
else if (menuChoice == 'n')
{
cout << "Please type your name, press ENTER when done." << endl;
cin >> playerName;
outFile.open("player.txt");
outFile << playerName;
outFile.close();
cout << "Welcome, " << playerName << ", the game will now begin!" << endl;
animalGuess();
}//endElseIf//
bool gameContinue();
system ("pause");
return 0;
}//endof main() function//
//Function that displays hints and allows player guess...//
void animalGuess()
{
//Function variables...//
string guess;
string hint1 = "I have four legs, what am I?";
string hint2 = "Incorrect. I have fur, what am I?";
string hint3 = "Incorrect. I have a tail, what am I?";
string hint4 = "Incorrect. I have whiskers, what am I?";
string hint5 = "Incorrect, last guess! I'm \"King of the Jungle\"\n What am I?";
int count = 0;
//Display hints, allow input of guess, increase count by one (after each guess input), and display count//
while (count < 5 && guess != animal)
{
cout << hint1 << endl;
cin >> guess;
count++;
cout << count << endl;
cout << hint2 << endl;
cin >> guess;
count++;
cout << count << endl;
cout << hint3 << endl;
cin >> guess;
count++;
cout << count << endl;
cout << hint4 << endl;
cin >> guess;
count++;
cout << count << endl;
cout << hint5 << endl;
cin >> guess;
count++;
cout << count << endl;
cout << "No more guesses remain. Sorry, you lose!" << endl;
cout << endl;
if (guess == animal)
{
void WinGame();
}//endIF//
else if (count == 5)
bool gameContinue();
}//endWhile//
}//end of animalGuess() function//
//Function that reports a winning game to player//
void WinGame()
{
string playerName;
cout << "Congratulations, " << playerName << ", you guessed correct!"
<< "The animal was a \"Lion\" " << endl;
}//end of WinGame() function//
//Function that returns continue value for another game; yes = true, no = false and quit//
bool gameContinue()
{
char contGame;
cout << "Play again? Enter: yes, to play again, otherwise, enter: no, to quit" << endl;
cin >> contGame;
if(contGame == 'yes')
{
return true;
}
else
{
return false;
}
}
|