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.

Edited 6 Years Ago by vidit_X: n/a

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?

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

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.

Edited 6 Years Ago by vidit_X: n/a

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?

Edited 6 Years Ago by ellaine101591: i forgot the explaination of my code

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!

Edited 6 Years Ago by peter_budo: Do not use TEXT tag while posting!

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;
}
This article has been dead for over six months. Start a new discussion instead.