Hello! i'm only a beginner in programming and only have a few idea about creating a program. We have an assignment to create a hangman:
Write a program that plays the game of Hangman. The program should pick a word(which is coded directly into the program) and display the following:
Guess the word: xxxxxx
Each x represents a letter. The user tries to guess the letters in word. The appropriate response yes or no should be displayed after each guess. After seven incorrect guesses, the user should be hanged. The display should look as follows:
O
/ l \
l
/ \
After each guess, display all user guesses. If the user guesses the word correctly, the program should display

Congratulations!!! You guessed my word. Play again? yes/no

You coded something?

Try it yourself first, if you have any problem then we'll help.

here is my initial code:

#include<iostream>
using namespace std;
int main()
{
const int arraySize=5;
char wordToBeGuessed[arraySize]={cat,dog,ant,pig,bird};
char theWord

cout<<"Enter your guess:";
cin   >>theWord;

uhm.. I want to make it step by step as possible so i can figure out my mistake. I have planned first to present the words to be guessed in the array theWordToBeGuessed. And prompt the user his guesses. How can I match his guess which is read as theWord in my program, to theWordToBeGuessed?

First of all, you cant have array of strings in char array. You need to have a 2D char array or a 1D string array like this ..

char wordToBeGuessed[][10]={cat,dog,......};

where 10 is the maximum size of any word in the list.

Now, before asking user to input a word, choose a random word out of the list. You can use rand() for that ..

int i=rand() % X;

where X is the number of words in the list.

And you'll need to input a single character and then check whether that letter is in the selected word until either the number of tries end or word is guessed correctly.

thanks! Here is, i presumed to be better than what i post yesterday. =)

#include<iostream>
#include<cstdlib>
#include<time>

void drawTheHangman(int drawing)

int main()
{
int theWord;
char theWords[][5]={"pig","dog","cat","bird","ant"};
int drawing=1;
char answer;


srand(time(NULL));
theWord=rand()%5;

for(int subscript=0; subscript<5;subscript++)
{
   theWords[subcript]='x';
}

while(drawing!=8)
{
   drawTheHangman(drawing);
   cout<<"Guess the word:";
   cin  >>answer;

   if(answer!=theWords)
     {
         drawing++
     }
}

void drawTheHangman(int drawing)
{
   if(drawing==8)
   {
       cout<< "   0    " <<endl;
              << " / l \   "<<endl;
              << "   l      "<<endl;
              << " /  \    " <<endl;
   }

   if(drawing==7)
   { 
       cout<< "   0    " <<endl;
              << " / l \   "<<endl;
              << "   l      "<<endl;
              << " /       " <<endl;
   }
   if (drawing==6)
    {
       cout<< "   0    " <<endl;
              << " / l \   "<<endl;
              << "   l      "<<endl;
              << "          " <<endl;
   }

   if(drawing==5)
    {
       cout<< "   0    " <<endl;
              << " / l \   "<<endl;
              << "          "<<endl;
              << "         " <<endl;
   }

   if (drawing==4)
    {
       cout<< "   0    " <<endl;
              << " / l     "<<endl;
              << "          "<<endl;
              << "         " <<endl;
   }
   if (drawing==3)
    {
       cout<< "   0    " <<endl;
              << "   l     "<<endl;
              << "          "<<endl;
              << "         " <<endl;
   }
    if(drawing==2)
    {
       cout<< "   0    " <<endl;
              << "         "<<endl;
              << "          "<<endl;
              << "         " <<endl;
   }
    if (drawing==1)
     {
       cout<< "          " <<endl;
              << "          "<<endl;
              << "          "<<endl;
              << "          " <<endl;
   }

}

Its only my initial code. If the guess of the user doesn't match with my word, the # of tries should increase until it reaches to 8 to stop the looping.. I should prompt the user for another guess, how can i show this?

Does that compile? You need to write short sections of code, compile and test each as you go. Otherwise when you get 800 lines of uncompiled/untested code, you will have a nightmare of errors to try to fix.

I did not compile it yet. But your right I should write only few section of code, compile it first and correct if there is mistakes. Before I continue to write another code. Thanks!

in your if statement when you are outputting your picture of the hanging man you are using a \. with c++ that is an escape character and is used for special purposes line \n for newline. in order to output a single \ to the screen you would need cout << "\\"; .

Thanks for all the idea you have shared. Here is my code and I have this problem: When I execute it, the program will not continue and will give me an executable error.
Here is my code:

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>


using namespace std;

int main()
{
    int theWord;
    string words[20] = {"array","pointer","class","string","static","vector","constructor","prototype","mnemonic",
                        "compiler","programming","structure","acessor","mutator","sentinel","public",
                        "protected","private","flowchart","constant"};
    
    string answer;
    vector<string>guess;
    int mistake=0;
    
    theWord=rand()%20;
    
    string a = words[theWord];
    cout<<a<<endl;
    cout<<"The length of the word is "<<a.length()<<" characters"<<endl;
    cout<<"Guess the word: ";
    
    for (int i=0; i<a.length(); i++)
    {
     cout<<'x'; 
    }
    
    cout<<"\nEnter your guess: ";
    cin >>answer;
    
    
    guess.push_back(answer);
    
    cout<< "You entered: "<< endl;
    for (int i=0; i<=7; i++)
    {
        cout<< guess[i]<<" ";
        cout<< endl;
    }
    
    
    
    if (answer==a)
    {
       cout<<"Congratulations!!! You guessed my word."<<endl;
    }
    else
    {
        mistake++;
    }
    
    
    
    system("PAUSE");
    return 0;
}

beginners can try this...hangman game ...its damm easy!!

#include<iostream>
#include<cstdlib>
#include<string.h>
using namespace std;
void drawing(int draw);
int main()
{   string theword;
    int randword;
    string words[22]={"fat","bat","mat","sat","dad","lad","cat","mom","bun","sun","fun","run","rat","tom","cop","mop","man","van","hat","red","app","sap"};
    int draw=1;
    string ans;
    randword=rand()%22;
    theword=words[randword];
    for(int i=0;i<22;i++)
    {words[i]='x';
    }cout<<"\t\t\t\tWelcome To Hangman\n";
    cout<<"\t\t\t\t********************\n";
    cout<<"The word list is:\nfat\tbat\tmat\tsat\tdad\tlad\tcat\tmom\tbun\tsun\tfun\trun\trat\ttom\tcop\tmop\tman\tvan\that\tred\tapp\tsap\n";
    cout<<"\nguess the right word\n";
while(draw<=8)
{   drawing(draw);
    cout<<"\n\nguess the word:\n";
    cin>>ans;
  if(ans==theword)
    {cout<<"\n\nCongratulations!!! You guessed my word.\n\n"<<endl;
    break;
    }else
    {   cout<<"\n\nTry it out again\n\n";
    draw++;
    }randword=rand()%22;
}return 0;
}void drawing(int draw)
{if(draw==1)
    {   cout<<"\t _________\n\t|\t |\n\t|\t O \n\t|\t/|\\\t\t\n\t|\t |\n\t|\t/ \\ \n\t|__";
    }else if(draw==2)
    {cout<<"\t _________\n\t|\t |\n\t|\t O \n\t|\t/|\\\t\t\n\t|\t |\n\t|\t/  \n\t|__";
    }   else if(draw==3)
    {cout<<"\t _________\n\t|\t |\n\t|\t O \n\t|\t/|\\\t\t\n\t|\t |\n\t|\t \n\t|__";
    }else if(draw==4)
    {cout<<"\t _________\n\t|\t |\n\t|\t O \n\t|\t/|\\\t\t\n\t|\t \n\t|\t\n\t|__";
    }   else if(draw==5)
    {cout<<"\t _________\n\t|\t |\n\t|\t O \n\t|\t/|\n\t|\t \n\t|\n\t|__";
    }else if(draw==6)
    {cout<<"\t _________\n\t|\t |\n\t|\t O \n\t|\t/\t\n\t|\t \n\t|\t \n\t|__";
    }   else if(draw==7)
    {cout<<"\t _________\n\t|\t |\n\t|\t O \n\t|\t\t\t\n\t|\t \n\t|\n\t|__";
}else if(draw==8)
{cout<<"\t _________\n\t|\t |\n\t|\t  \n\t|\t\n\t|\t \n\t|\n\t|__";
cout<<"\n\nshit!!!...u r hanged\n\n";
cout<<"\n ok u can try once for the peace of his sole!!!...XD\n";
}}

guys can u plz suggest fe some modifications.

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.