Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the assignment: For this assignment your are to write a program, using s

ID: 3637965 • Letter: H

Question

Here is the assignment:
For this assignment your are to write a program, using strings (from the string class) that plays
the popular game of Hangman. See the Wikipedia definition if
you have never played the game: http://en.wikipedia.org/wiki/Hangman_(game).

Here is how the program should work:
The word, phrase or sentence will be read in from a file called hangman.txt.
There will be only one word, phrase or sentence per line. The user guesses
will be done via the keyboard.
(1) The program should display the line by hiding all letters (a-z, A-Z)
with a '-' (dash). All other characters should be displayed normally.
(2) The program should ask the player to make a guess.
(3) If the player makes a correct guess then the program should
redisplay the word, phrase or sentence with the letter displayed and ask
the player to make another guess
(4) If the palyer makes an incorrect guess then the program should tell
the player the guess was incorrect, subtract a guess and tell the user how
many guesses remain.
(5) If no more guesses remain then the program should print a message stating
that the game is over and displaying the original word, phrase or sentence.
(6) If more guesses remain then the program should ask the player to guess
again.

The compilers in my computer lab are borland and ms visual studio 2012.

Explanation / Answer

please rate - thanks

#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
#define MAX_GUESSES 7
enum done{CONTINUE, WON ,LOST};
int good_guess(string,char);
void printword(string);
char getletter();

int main()
{
int i,used=0,guesses=0,blanks=0;
string Word;
string word_so_far;
char guess;
bool guessed=false;
done won;
ifstream input;
   input.open("hangman.txt");           //open file
   if(input.fail())             //is it ok?
       { cout<<"word file did not open please check it ";
        system("pause");
        return 1;
        }  
getline(input,Word);       
while(input)
{cout<<"Starting a new game My word has "<<Word.length()<<" characters. ";
used=0;
guess=0;
won=CONTINUE;
guesses=0;
guessed=false;
word_so_far="";
word_so_far.insert(0,Word.length(),'-');
for(i=0;i<(int)Word.length();i++)
     if(Word[i]==' ')
           {word_so_far[i]=' ';
           blanks++;
           }
cout<<"User word is ";       
printword(word_so_far);
while(won==CONTINUE)
    {
    guess=getletter();
     if(good_guess(Word,guess)==1)
         {i=0;
         i=Word.find(guess,i);
         while(i<(int)Word.length())
            {word_so_far[i]=guess;
              i=Word.find(guess,i+1);
              blanks++;
              }  
          
           cout<<"The word is now ";
          printword(word_so_far);
          if(blanks==Word.length())
               won=WON;
          }
     else
          {guesses++;
          cout<<"you have "<<MAX_GUESSES-guesses <<" guesses left ";
          }
     if(guesses==MAX_GUESSES)
           won=LOST;
     }

if(won==WON)
     cout<<"Congratulations YOU WON!! :) :) ";
else
    cout<<"sorry :( :( The word was "<<Word<<endl<<endl;   

input>>Word;
}
cout<<" No more words Hope you had fun! BYE for now ";
system("pause");
}


char getletter()
{char letter;
cout<<"Enter a letter: ";
cin>>letter;
return letter;
}
void printword(string w)
{
cout<<w;
cout<<" ";
}
int good_guess(string w,char let)
{if(w.find(let,0)<string::npos)
      return 1;
cout<<"Sorry, no occurrences of "<<let<<endl;
return 0;
}