Member Avatar for kohkohkoh

i have an assignment to do but i'm still not good at c++programming..
the question is:

--------------------------------------------------------------------------
Write a program in C++ that allows users to play the game of Hangman. The program stores a
series of words in a file called words.txt, and randomly chooses one word to be guessed by users
each time they play the game. Each user can make as many guesses as possible, as long as he/she
do not exceed 8 wrong guesses before he/she “is hanged. For each guess, draw the progress of
the “man being hanged. A user has the option to request for hints, but he/she is only allowed to
request a maximum of three hints per game.
The program also allows the users the option to add and delete a word in the file word.txt. But
this option is only allowed for an administrator, thus it needs a password before the user can
make any changes to the file. Ensure that your program maintains this level of security.
--------------------------------------------------------------------------

my questions are
1.how to store series of words in a file called words.txt?
2. how to let the computer randomly choose a word to be guessed by user each time he/she plays the game. I only know how to set the system randomly pick the numbers by using rand() in c++, but for words.. how and what should i do?
3. how to do the last part???
The program also allows the users the option to add and delete a word in the file word.txt. But
this option is only allowed for an administrator, thus it needs a password before the user can
make any changes to the file. Ensure that your program maintains this level of security.

HELP MEEEE....GUIDE NEEDED....

check the help files that came with your compiler. most have a tutorial of some kind. check up on file handling to start with.

FILE *fopen(char *filename,char *mode);

filename is name of file to be opened.
mode can be lots of things but you'll probably be using "r" for "reading" and "a" for "appending". the function returns a FILE pointer to the opened file or NULL if the file could not be opened. once opened you can use the variants of printf and scanf: fprintf and fscanf respectively. the only difference is that they take a file pointer as their first argument. remember to use

int fclose(FILE *pointer)

to close the file when you are finished.

there is no function that i (with my massive experience of nearly 4 days) know of to randomly select a line form a file. but then there is no need for one as you can just use rand() and a simple loop to do it for you. remember to make sure you are only selecting random numbers up to the number of words in the file - otherwise you could have problems and will need to come up with some untidy work-around - at the very least the last word will be used disproportionately often. so get the random numbers in the right range first.

not sure i can help you on the password though. i'm guessing you need one that the admin can alter at a later time right? i dunno. maybe use a really simple code and write the coded version to a file. maybe increase each of the ASCII values for each letter by its position in the password.

hope this gives you somewhere to start. on the other hand if i completely missed the point somewhere and have been talking gibberish then go easy on me cos i'm new to this.

hood luck with it.

Ideas...

1) You could create the words.txt file in some editor with one word per line, like this:

elephant
apple
cranberry
stick

Then use the standard c file library (as lordofthisworld suggested) to open it, read lines, and close it. Note that fgets() returns a whole text line, and in this case that would be one word.

2) You already know rand(), and you can use that to read a random number of words with fgets() in a loop. "r = rand(); for (i = 0; i < r; i++) fgets()...". Just keep in mind that you need to know how many lines there are in the file in order to constrain the random value. So, you may want to read throgh the file once to count the words.

3) The simplest thing to do to add or delete an entry is to COPY the file, adding the new entry or removing the old entry. Maybe to words.new, say. And then swap the files so the new file becomes the text file.

Member Avatar for kohkohkoh

thank you for

Here a file i/o tut (detailed :D)
http://www.daniweb.com/techtalkforums/thread6542.html

Once you have read a random word then you can go a head with the game

Look at something I wip up on the fly,may not be too correct

//headers

void show(char* s,bool *d,int len)
{
     cout<<"\n\n";

      for(int i=0;i<len;i++)
      {
          if(d[i])cout<<s[i];      //display the letter
          else cout<<"_"          //else the blank
      }
}

bool check(char ch,char* s,bool *d,int len)
{
      bool ret=false;

          for(int i=0;i<len;i++)
          {
                if(s[i]==ch)    ret = d[i] = true;         
          }
    return ret;
}

void main()
{
     char word[100]={"something"};
     char hng={"HANGMAN*"};
     char ch;

     int wrong=0,max_wrong = 8;
     int len = strlen(word);

     bool *disp = new int [len];
     for(int i=0;i<len;i++)
        disp[i] = false;



   do
     {
         show(word,disp,len);     //show the word
         cin>>ch;
          
        if(check(ch,word,disp,len))
        {
             cout<<"\tCool you got a letter";
        }
         else
         {
             cout<<"\tSorry,wrong";
             wrong++
         }
     }

  
  }   
  while (wrong <= max_wrong);

   cout<<"Thankyou Bye";
}

Ok it's go a few things missing for one thing it does not show you if you won or lose or choose a random word etc.I just wanted to show a way to display the stuff and hide letters.

Help?

Hi,
I need to write a hangman game using these specif rules shown below. Is there a way you can use pointers to identify a specific element in a string?

Using a class, write a program that plays the game of Hangman. A string
data member is used to store the secret word (i.e. the word must be at least
8 characters long and is case insensitive). More than one data member is
allowed. A class constructor initialize a secret word. A set member function
is provided so to allow changing the secret word. All string manipulations are
to be performed using the string class member functions (i.e. no character
arrays are allowed). The program displays the following:
Guess the word: = = = = = = = =
= represents a letter. The number of =’s represents the length of the secret word. A
Part III
user is allowed to guess individual letters of the secret word or the entire secret
word. After each user’s guess, the program displays ALL the user’s guesses and
one of the following responses:
After a correct guess, the program shows where the letters fit in the secret word
After each incorrect response another body part of the Hangman is displayed.
After 7 wrong guesses (i.e. body parts). The program displays:
You lost! – Play again? [y/n]: (the secret word is displayed if n is entered)
If the user guesses the word correctly, the program displays:
Congratulation you won! – Play again? [y/n]:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.