![]() |
|
Welcome to the Computer Webmaster Gaming Console Graphics Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| |||||||
| Software Programming Software programming talk, ask questions about computer software programming or help others |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 | ||
| I am trying to make a simple game of Hangman. The following code does run when compiled. It is missing the "meat" of the game. What I need to figure out is how am I going to check to see whether the letter entered by player is in the answer or main word of the game. Oh and to run this code youll need a text file named words.txt in the same directory as the .exe. This is where it gets the words from. //June 2003 :: TempeR #include "iostream"//cin and cout i/o #include "fstream.h"//file stream #include "stdio.h"//fgets fflush //Function declarations void read_file(char gameword[]); char get_letter(); //Variable Declarations int wordlen=0; char word[15]; char char_input; int main(void) { cout<<"Welcome to HangMan"<<endl; cout<<"1. New Game"<<endl; cout<<"2. Exit"<<endl; cout<<"3. Restart Program"<<endl; int input; cin>>input; switch (input) //user selection menu { case 1: read_file(word); //testing the function results cout<<"This is the word.."<<word<< endl; wordlen = strlen(word)-1; //testing function results cout<<"This is the len..."<<wordlen<<endl; cout<<"Enter a letter between A - Z: "<<endl; char_input=get_letter(); //testing function results cout<<"You entered: "<<char_input<<endl; break; case 2: cout<<"Maybe next time."<<endl; return 0; case 3: cout<<"Restarting program..."<<endl; main(); default: cout<<"Invalid Choice :: I didn't say you could do that!"<<endl; cout<<endl; main(); } return 0; } void read_file(char gameword[]) // to do: randomly pick string from file. { cout<<"Reading from file..."<<endl; //open file for read fopen() --- stdio.h FILE *fp; fp = fopen("words.txt","r"); //read string from file fgets() char *line; line = fgets(gameword,15,fp); //first line of file stored in *char gameword } char get_letter() { char letter; cout<<endl; //make sure a letter is entered. do { cin>>letter; if (letter<65||(letter>90&&letter<97)||letter>122) //if they dont enter a letter then Loop cout<<"Please enter a letter from A to Z!"<<endl; } while (letter<65||(letter>90&&letter<97)||letter>122); //if they do enter a letter then make it lowercase if(letter>=65&&letter<=90) letter=97 +letter - 65; //changes fom uppercase to lowercase if(letter>=65&&letter<= 90) letter=97+letter-65; return (letter); } //to-do's: somehow check each letter of the word against the // letter the user entered. | |||
|
| Advertisements |
| | #2 | ||
| "TempeR" <temper@swfla.rr.com> wrote in message news:<1f3Ma.212818$4y6.3547822@twister.tampabay.rr .com>... > I am trying to make a simple game of Hangman. The following code does run > when compiled. It is missing the "meat" of the game. What I need to figure > out is how am I going to check to see whether the letter entered by player > is in the answer or main word of the game. Okay, to be honest, I didn't look at all your code or try to run it. What I did look at looked fine, though. Checking a letter against the word or phrase trying to be guessed is fairly straight-forward. All you need to do is check the character entered against the characters in the string. This is about how you would do it: char enteredCharacter; char stringToGuess[] = { "W", "o", "m", "b", "a", "t" }; // get user input here int guessLen = strlen( stringToGuess ); int numOccurances = 0; for( int i=0; i < guessLen; i++ ) { if( toupper(stringToGuess[i]) == toupper(enteredCharacter) ) ++numOccurances; } // check to see if they guessed correctly if( numOccurances > 0 ) // guess correctly method else // guess incorrectly method The toupper simply converts both characters to a case-insensitive compare. That's about it. | |||
|
| | #3 | ||
| Line 1 should be #include "iostream.h" I'm not quite sure how this compiled correctly. But, since it did, I suggest you use Frecklefoot's method. Since you want to convert things to lower-case, use tolower(); instead of toupper(); This chunk of code is really unnecessary then: while (letter<65||(letter>90&&letter<97)||letter>122); /*if they do enter a letter then make it lowercase*/ if(letter>=65&&letter<=90) letter=97 +letter - 65; //changes fom uppercase to lowercase if(letter>=65&&letter<= 90) letter=97+letter-65; return (letter); } I hope that I was of help :-) TempeR <temper@swfla.rr.com> wrote in message news:1f3Ma.212818$4y6.3547822@twister.tampabay.rr. com... > I am trying to make a simple game of Hangman. The following code does run > when compiled. It is missing the "meat" of the game. What I need to figure > out is how am I going to check to see whether the letter entered by player > is in the answer or main word of the game. Oh and to run this code youll > need a text file named words.txt in the same directory as the .exe. This is > where it gets the words from. > > //June 2003 :: TempeR > #include "iostream"//cin and cout i/o > #include "fstream.h"//file stream > #include "stdio.h"//fgets fflush > > //Function declarations > void read_file(char gameword[]); > char get_letter(); > > //Variable Declarations > int wordlen=0; > char word[15]; > char char_input; > > > int main(void) > { > cout<<"Welcome to HangMan"<<endl; > cout<<"1. New Game"<<endl; > cout<<"2. Exit"<<endl; > cout<<"3. Restart Program"<<endl; > int input; > cin>>input; > switch (input) //user selection menu > { > case 1: > read_file(word); > > //testing the function results > cout<<"This is the word.."<<word<< endl; > > wordlen = strlen(word)-1; > > //testing function results > cout<<"This is the len..."<<wordlen<<endl; > > cout<<"Enter a letter between A - Z: "<<endl; > char_input=get_letter(); > > //testing function results > cout<<"You entered: "<<char_input<<endl; > > > break; > case 2: cout<<"Maybe next time."<<endl; > return 0; > case 3: cout<<"Restarting program..."<<endl; > main(); > default: > cout<<"Invalid Choice :: I didn't say you could do that!"<<endl; > cout<<endl; > main(); > } > > > return 0; > } > > void read_file(char gameword[]) // to do: randomly pick string from > file. > { > cout<<"Reading from file..."<<endl; > //open file for read fopen() --- stdio.h > FILE *fp; > fp = fopen("words.txt","r"); > //read string from file fgets() > char *line; > line = fgets(gameword,15,fp); //first line of file stored in > *char gameword > } > > char get_letter() > { > char letter; > cout<<endl; > //make sure a letter is entered. > do > { > cin>>letter; > if (letter<65||(letter>90&&letter<97)||letter>122) //if > they dont enter a letter then Loop > cout<<"Please enter a letter from A to Z!"<<endl; > } > while (letter<65||(letter>90&&letter<97)||letter>122); //if > they do enter a letter then make it lowercase > if(letter>=65&&letter<=90) > letter=97 +letter - 65; > //changes fom uppercase to lowercase > if(letter>=65&&letter<= 90) > letter=97+letter-65; > return (letter); > } > > //to-do's: somehow check each letter of the word against the > // letter the user entered. > > | |||
|
| Featured Websites | ||||
|
![]() |
| Tags: code, help |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| MapSource Unlock Code | Wayne R. | GPS | 0 | 06-12-2007 5:12 PM |
| Some code required | Simon B. Hansen | Database | 0 | 05-31-2007 8:37 PM |
| What's wrong with this code please? | Big Bill | Website Reviews And Website Questions | 13 | 05-28-2007 1:13 AM |
| BB code vs HTML | modric | HTML | 3 | 09-29-2006 2:25 PM |
| Handy Code Snippets | Whatcha | PHP | 1 | 08-11-2006 12:16 AM |
| Featured Websites | ||||
|