Computer Webmaster Gaming Console Graphics Forum

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.

MK PitStop Main Earn $25 Earn Money Posting Extras Members Blogs Image Hosting User Pages
Go Back   Computer Webmaster Gaming Console Graphics Forum > Computer Forums > Software Programming
Register FAQ/Rules Become A V.I.P. Member Search Today's Posts Mark Forums Read

Software Programming Software programming talk, ask questions about computer software programming or help others

Google
Reply
 
LinkBack Thread Tools Display Modes
Old 06-12-2007, 9:39 PM   #1
TempeR
 
TempeR's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default help with this code ...

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.


 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Advertisements
Old 06-12-2007, 9:39 PM   #2
Frecklefoot
 
Frecklefoot's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default help with this code ...

"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.
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 06-12-2007, 9:41 PM   #3
Mario
 
Mario's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default help with this code ...

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.
>
>



 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Featured Websites
Free Space
Free Space
Free Space Free Space
Reply
Tags: ,




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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




All times are GMT +1. The time now is 8:10 PM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0
Cheap Computers
MK PitStop Copyright 2005 - 2008

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