Sweet! Did yah get it working?
----------------------------------
then how do u have a function work properly without it calling itself?
Yeah, if you have a situation where you feel like your function needs to call itself then you probably want a loop around that code. I copied a bunch of your older code and added in loops so you can see what I'm talking about. Basically, it removes the parts where a function calls itself and the LABEL/GOTO's with a loop. I highlighted so you can spot them real quick without having to re-read your code.
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int GameStart();
//int PlayerInput();
int HelpAndGuide();
//int PlayerNames();
int main()
{
int menuChoice = 0; //what type of meanu they want
bool exitGame = false;
while(exitGame == false){ // Keep the application going until the user says they are done.
system("cls");
cout << "Choose A Number.\n"; //Displays Menu Text
cout << "1. Play Game\n";
cout << "2. Help and Guide\n";
cout << "3. Exit\n";
cout << "Choice:";
cin >> menuChoice;
switch ( menuChoice ) //Allows For Menu Option Selection
{
case 1:
GameStart();
break;
case 2:
HelpAndGuide();
break;
case 3:
exitGame = true;
break;
default:
GameStart();
break;
}
}
return 0;
}
int GameStart() //Labels The Game's Start
{
cin.get();
system("cls");
//string name; //Players Name (Replace with an array or list (see vector<> below) so that you can record each players name)
// This is just an array for holding player names. (ie string playerNames[10])
// The difference between a basic array and this vector<> is this vector has no limit. It can grow to meet demands.
vector<string> playerNames; // This will record every player's name currently in the game.
string players = "y"; //If theres more players
int playerCount = 0;
while(players == "y") // Keep prompting for more names as long as the user keeps replying yes.
{
string currName;
cout << "Player #" << playerCount + 1 << " Enter Your Name Here:";
cin >> currName;
playerNames.push_back(currName); // Store the players name in a list so you can remember it.
cout << "Welcome " << currName << "!!\n";
cout << "More Players?'y/n'"; //Allows For Multiple Player Notification
cin >> players;
playerCount = playerCount + 1; // Increment # of players.
}
system("cls");
cout << "Designate A Leader Of This Game Now.\n\n"; //Tells Players To Choose A Leader
//if (players=="y")
//{
// GameStart(); // <--- This is a case of that function recursion where a function keeps calling itself over and over. This behavior replaced with the above while loop.
//}
//else if(players=="n")
//{
//}
cin.get();
// The following is just the code from your PlayerInput() function.
//PlayerInput(); //Designates Time Of Game For Player Interaction
string gameword; //the designated word from the leader
int wordlength = 0; //to tell plyers how long the word is
bool validWord = false;
while(validWord == false) // Keep prompting the leader for a new word until a valid word is entered.
{
cout << "For Game Leader:\nEnter Game Word Here:";
getline(cin, gameword,'\n'); //leader of game enters the word of the game here
system("cls");
wordlength = gameword.length(); //stores the length of the word into "wordlength"
if(wordlength > 25)
{
cout << "The Word You Entered Was Too Long For This Game\n";
cout << "Please Try Another Word."; //checks to see if word will fit in the string allowed
}
else
{
cout << "CONGRATULATIONS! You Have Chosen A Valid Word\n";
cout << "Now You Are Ready To Play The Game\n\n\n"; //tells the leader he/she has chosen a valid word, and it's time to play
validWord = true;
}
}
int strikes = 0; // to tell the plyers how many wrong guesses
string lost; //choice by player to play again
int lives = 10; //number of lives player has
string answer; //reference to the answer for the player's guess
string decision; //string for type of guessing
char guess; //what the players will be defining to play the game
string success; //to decide if guessed letter is present
int currPlayer = 1; //signify which players turn it is.
bool gameOver = false;
//char astring[30] = "-"; Might as well make this guy a string too... although no problem with using a char array.
string astring;
for(int a = 0; a < wordlength; a++)
{
astring = astring + '-'; // Fill the astring with the # of dashes needed (ie wordlength)
}
while(gameOver == false){ // Keep going round and round until a player wins or too many strikes, etc.
cout << "\nFor Players:\nThe Game Word Has A Length Of " << wordlength << " Letters\n\n"; //reveals the length of the game word to the players
cout << astring; // Display the "hidden word".
//for (char w=0;w<wordlength;w++)
//{
// cout << astring;
//}
cout << "\n\n(Player #" << currPlayer << ") " << playerNames[currPlayer - 1] << ", What Would You Like To Do?\n";
cout << "Type 'Answer' To Try And Guess The Word Or Type 'Turn' To Guess A Letter.";//Asks Player To Try Choose Between Guessing A Letter Or The Word
cin >> decision;
if (decision=="turn")//Goes Here If Player Wants To Guess Letters
{
system("cls");
cout << "Type Your Single Letter Guess Here:";
cin >> guess;
success="n"; // Probably want to replace these kinds of things with a true or false boolean (ie bool success = false)
for(int i = 0; i < wordlength; i++)//Checks For Players Guess Validity
{
if(gameword[i] == guess)
{
success="y";
cout << "Letter " << guess << " Is In Position " << i + 1 << " In The Game Word\n\n"; //Displays Location Of Valid Guess
// If I were you I'd probably reveal the letter to the player here by storing in that string containing all dashes.
astring[i] = guess;
}
}
if (success=="n")
{
cout << "Sorry, The Letter You Have Guessed Was Incorrect.\n"; //Tells Player Their Guess Was Not In The Word
strikes = strikes + 1;
cout << "Lives Remaining " << lives - strikes << "\n";//Takes Away Srikes From Total Initial Lives
if(lives-strikes <= 0)//Check For Loss
{
// Multiple ways to handle this. In this case it simple goes back to main menu, but you could use a loop
// within this function near the top to keep the game going/start fresh.
cout << "YOU HAVE LOST!\n\n";
//cout << "YOU HAVE LOST!\nTry Again?'y/n'";//Reveals Loss
//cin >> lost;
gameOver = true; // Leave this function and return to the main function.
//if (lost=="y")
//{
// main(); //Goes To Beginning of Game, If Chosen
//}
//else
//{
// exit(0);
//}
}
}
else // The guess made was valid. Check to see if the entire word has been revealed.
{
// And then here you can check to see if the gameword matches.
if(astring == gameword)
{
gameOver = true;
cout<<"CONGRATULATIONS! You Have Found The Word!\nYou Are Now The Game Leader!\n\n";//Reveals Victory
cout << "The word was: " << gameword << "\n\n";
}
}
//for(int i = 0; i < wordlength; i++)
//{
// if (astring[i]==guess)
// {
// cout<<astring;
// if (astring==gameword)
// {
// goto Win;
// }
// }
// /*if(astring[i]==guess)
// {
// cout<<astring;
// }
// if (astring[i]!=guess)
// {
// cout<<" ";
// }*/
//}
}
else if (decision=="answer")//Goes Here If Player Wants To Guess The Word
{
system("cls");
cout<<"What Do You Think The Answer Is?";//Asks Player For The Guess Of What The Word Is
cin>>answer;
if( answer==gameword)
{
//Win: Took away the GOTO:LABEL that everyone seems to hate. :)
system("cls");
gameOver = true;
cout<<"CONGRATULATIONS! You Have Found The Word!\nYou Are Now The Game Leader!\n\n\n";//Reveals Victory
//cout<<"Hit Enter To Start New Game";
}
else
{
cout<<"Sorry, The Word You Have Guessed Was Incorrect.\n";
strikes=strikes+2;
cout<<"Lives Remaining "<<lives-strikes<<"\n";
if(lives-strikes <= 0)
{
cout << "YOU HAVE LOST!\n\n";
gameOver = true;
//cout<<"YOU HAVE LOST!\nTry Again?'y/n'";
//cin>>lost;
//if (lost=="y")
//{
//GameStart();
//}
//else
//{
//exit(0);
//}
}
}
}
currPlayer = currPlayer + 1; // Next players turn.
if(currPlayer > playerCount) // Oops, no more players...first players turn again.
currPlayer = 1;
}
cout << "Hit Enter To Continue";
cin.get(); // Been a while since I programmed in "console" mode, but there is probably a way to "flush"
// the buffer instead of having to put two cin.get()'s.
cin.get();
return 0;
}
int HelpAndGuide() //Goes Here From 2nd Menu Selection And Provides Game Information
{
system("cls");
cout<<"This Is The Consol Version Of HangMan!\n";
cout<<"You Have A Space Of 25 Letters For The Game Word.\n";
cout<<"When Playing This Game And Taking Guesses, A Wrong Guess Will Result\nIn A Strike.\n";
cout<<"Also When Guessing Letters, The Letter You Type As Your Guess Will.\n";
cout<<"Result In A Number Or Numbers Of Position(s) That The Letter Is.\n";
cout<<"That Number Is A Reference To The Location Inside The Game Word.\n";
cout<<"You Have 10 Chances, Including Both Letter Guesses And Full Word Guesses.\n";
cout<<"Letter Guesses Are Worth 1 Life\n While Word Guesses Are Worth 2 Lives.\n";
cout<<"After 10 Chances Are Gone, You Will Lose.\n";
cout<<"When Typeing Anything In This Game, Type Exclusively In Lowercase.\n\n\n\n\n";
cout<<"Hit Enter";
cin.get();
//GameStart(); // Doesn't start the game here instead goes back to main menu.
cin.get();
return 0;
}