I'm New...=(

This is a discussion on I'm New...=( within the C++ Programming forums, part of the General Programming Boards category; Like everyone has been telling you: Code: int GameStart(); int main() { int strikes=0; // to tell the plyers how ...

  1. #31
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Like everyone has been telling you:
    Code:
    int GameStart();
    
    int main()
    {
    	int strikes=0; // to tell the plyers how many wrong guesses
    	int wordlength; //to tell plyers how long the word is
    	char guess; //what the players will be defining to play the game
    	string gameword; //the designated word from the leader
    	string decision; //string for type of guessing
    	string answer; //reference to the answer for the player's guess
    	string lost; //choice by player to play again
    	int menuchoice; //what type of meanu they want
    	string success; //to decide if guessed letter is present
    	string name; //Players Name
    	string players; //If theres more players
    	int p; //number of players
    	int lives=10; //number of lives player has
    	char astring[30]="-";
    
    	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:
    		exit(0);
    		break;
    	default:
    		GameStart();
    		break;
    	}
    }
    
    int GameStart()
    {
    You can't go on like this, you know... If you haven't learned functions, it's obvious you lack a lot of basics. You need a book. Do not proceed until you know the basics. It will confuse and you will get nowhere.
    C++ is not for beginners.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #32
    Registered User
    Join Date
    May 2008
    Posts
    18
    it's not exactly that i'm a beginner, i'm fairly new to c++ programming. but i just kept getting responses on what to type just not the positioning of each.

  3. #33
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    That's why you should get a book.
    You may not be a beginner at something else, but in C++, you are and C++ is mostly a low level language that requires a solid understanding to use.
    You will do yourself a favour to get a good book. Trust in that. When you read one, you will understand so much more.
    Why need to struggle so much because you don't understand? If you properly learn the basics, you won't have that problem anymore!

    I'm not the only one who will tell you that. Pretty much all around here has ready one or two books about C/C++ to learn it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #34
    Registered User
    Join Date
    Jun 2007
    Posts
    41

    Thumbs down

    ouchies you guys are rough... confusing the poor lad.

  5. #35
    Registered User
    Join Date
    May 2008
    Posts
    18
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int GameStart();
    int PlayerInput();
    int HelpAndGuide();
    int PlayerNames();
    int main()
    {
    	int menuchoice; //what type of meanu they want
    
    	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:
    		exit(0);
    		break;
    	default:
    		GameStart();
    		break;
    	}
    }
            int GameStart() //Labels The Game's Start
            {
    		cin.get();
    		system("cls");
    
            string name; //Players Name
            string players; //If theres more players
    
    			cout<<"Enter Your Name Here:";
    			cin>>name;
    			cout<<"Welcome "<<name<<"!!\n";
    			cout<<"More Players?'y/n'"; //Allows For Multiple Player Notification
    			cin>>players;
    			if (players=="y")
    			{
    				GameStart();
    			}
    			else if(players=="n")
    			{
    				system("cls");
    				cout<<"Designate A Leader Of This Game Now.\n\n"; //Tells Players To Choose A Leader
    			}
    
    		cin.get();
    		PlayerInput();
            }
            int PlayerInput() //Designates Time Of Game For Player Interaction
    		{
    		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
            string gameword; //the designated word from the leader
            int wordlength; //to tell plyers how long the word is
            char astring[30]="-";
            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
    
    		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
    		}
    		cout<<"\nFor Players:\nThe Game Word Has A Length Of "<<wordlength<<" Letters\n\n"; //reveals the length of the game word to the players
    
    		for (char w=0;w<wordlength;w++)
    		{
    			cout<<astring;
    		}
    
    			cout<<"\n\nWhat 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";
    				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
    						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<<" ";
    							}*/
    						}
    
    					}
    				}
    
    				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(strikes==lives)//Check For Loss
    					{
    						cout<<"YOU HAVE LOST!\nTry Again?'y/n'";//Reveals Loss
    						cin>>lost;
    						if (lost=="y")
    						{
    							main(); //Goes To Beginning of Game, If Chosen
    						}
    						else
    						{
    							exit(0);
    						}
    					}
    				}
    			}
    			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:
    					system("cls");
    					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";
    					cin.get();
    					GameStart();
    				}
    				else
    				{
    					cout<<"Sorry, The Word You Have Guessed Was Incorrect.\n";
    					strikes=strikes+2;
    					cout<<"Lives Remaining "<<lives-strikes<<"\n";
    					if(strikes==lives)
    					{
    						cout<<"YOU HAVE LOST!\nTry Again?'y/n'";
    						cin>>lost;
    						if (lost=="y")
    						{
    							GameStart();
    						}
    						else
    						{
    							exit(0);
    						}
    					}
    				}
    			}
    
    	PlayerInput();
    		}
    	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();
    
    	cin.get();
        }

    OK, functions are working!

    but if you would copy and run that, it doesn't work on the "turn" decision to guess a letter, instead it jumps back out to the "Congratulations..." blah blah... i'm working on it as well, i'd like to see what u find b4 i can tho.

  6. #36
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    I think they got you in a pickle by telling you to use functions

    You better hide or they might start badgering you again.

    You have a bad case of what they call function recursion where unfortunately one of your functions keeps calling itself over and over.. that being your PlayerInput() function.

    Code:
    int PlayerInput()
    {
      ... All your guess game logic, etc.
    
      PlayerInput();  <-- Calls itself, which is not equivalent to the GOTO:LABEL system.  In this case any of the stuff you setup (Gameword, astring, etc.) are lost [ERASED! :devil:], which I'm sure among some other minor things will make things very unpleasant for you.
    }
    


    They are also going to say you shouldn't call the Main() function in your code. But, mostly.. unfortunately you have a lot of functions within functions within functions (a circular logic) that will really make things difficult for you to manage. I think they confused you by telling you to use functions.


  7. #37
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Quote Originally Posted by HyperShadow View Post
    They are also going to say you shouldn't call the Main() function in your code. But, mostly.. unfortunately you have a lot of functions within functions within functions (a circular logic) that will really make things difficult for you to manage. I think they confused you by telling you to use functions.
    Not hard at all. If you need to call the code in main, break out that code into its own function and call that instead of main.
    And functions are basics, really. Everyone should know them, and if not, then they really should go back to the book because you won't get anywhere without functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #38
    Registered User
    Join Date
    Jun 2007
    Posts
    41

    Talking

    Functions are easy once you know how to use them, but don't you remember the n00blet days? For about a year I included #include <iostream> in my console projects just because my teacher told me to . One can write a perfectly good program with no functions at all and then afterwards use the finished program and start tweaking it to learn the basic concepts of programming. (replace the code with functions, classes, etc) You guys are sending the poor lad into a maze with no map he might get lost and never find his way back out.

    (If his brain explodes, it wasn't me )

  9. #39
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    I think at the current stage thisisurlife08 is at, you should know functions.
    Otherwise you shouldn't tackle such large projects.
    If you don't know them... well, step back, read about them, practice, then continue.
    C++ isn't a beginner's game.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    You guys are sending the poor lad into a maze with no map he might get lost and never find his way back out.
    That happened the moment thisisurlife08 used goto. Aside from a few special cases, goto is a maze with no map in which you will get lost and never find your way back out.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #41
    Registered User
    Join Date
    Jun 2007
    Posts
    41

    Talking

    Quote Originally Posted by laserlight View Post
    That happened the moment thisisurlife08 used goto. Aside from a few special cases, goto is a maze with no map in which you will get lost and never find your way back out.
    Ha! Well played.

  12. #42
    Registered User
    Join Date
    May 2008
    Posts
    18
    see the only problem with what ur saying is that i understand goto's much more than functions, i do NOW understand functions, alot more than i did say 2 days ago... but... functions have cause my game to now not work... and it frustrating...

  13. #43
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    But your gotos would like have made it even more difficult to write your program in the end.
    You will face a lot of situations with frustration. Programming isn't easy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #44
    Registered User
    Join Date
    May 2008
    Posts
    18
    never really expected it too, that's what college is for...

    but why does my program restart after inputing a value for guess... i don't see where it gets confused. and why it goes back to there...

    i saw hypershadow saying that a function calling itself isn't the equivalent to a goto... then how do u have a function work properly without it calling itself?

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int GameStart();
    int PlayerInput();
    int HelpAndGuide();
    int PlayerNames();
    int main()
    {
    	int menuchoice; //what type of meanu they want
    
    	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:
    		exit(0);
    		break;
    	default:
    		GameStart();
    		break;
    	}
    }
            int GameStart() //Labels The Game's Start
            {
    		cin.get();
    		system("cls");
    
            string name; //Players Name
            string players; //If theres more players
    
    			cout<<"Enter Your Name Here:";
    			cin>>name;
    			cout<<"Welcome "<<name<<"!!\n";
    			cout<<"More Players?'y/n'"; //Allows For Multiple Player Notification
    			cin>>players;
    			if (players=="y")
    			{
    				GameStart();
    			}
    			else if(players=="n")
    			{
    				system("cls");
    				cout<<"Designate A Leader Of This Game Now.\n\n"; //Tells Players To Choose A Leader
    			}
    
    		cin.get();
    		PlayerInput();
            }
            int PlayerInput() //Designates Time Of Game For Player Interaction
    		{
    		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
            string gameword; //the designated word from the leader
            int wordlength; //to tell plyers how long the word is
            char astring[30]="-";
            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
    
    		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
    		}
    		cout<<"\nFor Players:\nThe Game Word Has A Length Of "<<wordlength<<" Letters\n\n"; //reveals the length of the game word to the players
    
    		for (char w=0;w<wordlength;w++)
    		{
    			cout<<astring;
    		}
    Turn:
    			cout<<"\n\nWhat 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";
    				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
    						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<<" ";
    							}*/
    						}
    
    					}
    				}
    
    				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(strikes==lives)//Check For Loss
    					{
    						cout<<"YOU HAVE LOST!\nTry Again?'y/n'";//Reveals Loss
    						cin>>lost;
    						if (lost=="y")
    						{
    							main(); //Goes To Beginning of Game, If Chosen
    						}
    						else
    						{
    							exit(0);
    						}
    					}
    				}
    			}
    			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:
    					system("cls");
    					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";
    					cin.get();
    					GameStart();
    				}
    				else
    				{
    					cout<<"Sorry, The Word You Have Guessed Was Incorrect.\n";
    					strikes=strikes+2;
    					cout<<"Lives Remaining "<<lives-strikes<<"\n";
    					if(strikes==lives)
    					{
    						cout<<"YOU HAVE LOST!\nTry Again?'y/n'";
    						cin>>lost;
    						if (lost=="y")
    						{
    							GameStart();
    						}
    						else
    						{
    							exit(0);
    						}
    					}
    				}
    			}
        goto Turn;
    		}
    	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();
    
    	cin.get();
        }
    this is what i've implemented, i now have a small number of 2 goto's(i believe, i know i have 2 labels) it works so far, and i think i'm going to be done with it? unless you have a better way of doing it.

    now, my training has be non-formal, and is 95&#37; off this website(cprogramming.com, and this forum), plus my dad helps me occasionally. i have also read ONLY ONCE EACH, the tutorials up to typecasting. i wanted to see my abilities from that, and go from there. by no means do i call my self experienced with this language, but i feel good with what i've accomplished in just a few days of learning.
    Last edited by thisisurlife08; 05-13-2008 at 02:51 PM.

  15. #45
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    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;
    }
    Last edited by HyperShadow; 05-13-2008 at 04:20 PM.

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21