View Single Post
Old 06-12-2007, 9:39 PM   #1
TempeR
 
TempeR's Avatar
 
Posts: n/a
My Photos: ()

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.


  Reply With Quote
Advertisements