hangman

This is a discussion on hangman within the C++ Programming forums, part of the General Programming Boards category; hi guys... I have to write a programme that plays a game of hangman with following feeatures: 1.the programme start ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Unhappy hangman

    hi guys...

    I have to write a programme that plays a game of hangman with following feeatures:

    1.the programme start by reading from an input file a list of words and stores them in an array of string.
    2.the programme then selects randomly one of the words. the word should appears like *****
    3.the programme should includes 3 functions:
    a. function to read the list of words.
    b. function to find the number of occurrences and locations of a guessed letter
    c.test if a game has terminated (6 incorrect guesses attempted).

    sample output:

    hangman
    the word: **
    guess a letter: f
    not exists
    **
    guess a letter: h
    h exists 1 time in the word
    h*
    guess a letter: i
    i exists 1 time in the word
    hi
    bravo

    play again? (Y/N): N
    thank you
    Last edited by alshidi; 01-01-2011 at 12:59 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    read the homework policy and show what you've done
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Since you missed this the first time around, here it is again.
    C Board - Announcements in Forum : C++ Programming

    > I need your help today ,please.
    No kidding -> How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    I am sorry , I just need some hints.



    I modified the following code:

    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <ctime>
    #include <cstdlib>
    #include <cctype>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	char my_choice;
    const int MAX_WRONG = 6;
    vector<string> words;
    words.push_back("guess");
    words.push_back("hangman");
    words.push_back("diffecult");
    
    srand(time(0));
    random_shuffle( words.begin(), words.end() );
    const string THE_WORD = words[0];
    int wrong = 0;
    string soFar ( THE_WORD.size() , '*' );
    string used = " ";
    
    cout << "Hi, let's play hangman.\n";
    
    while ( ( wrong < MAX_WRONG) && ( soFar != THE_WORD ) )
    {
    
    cout << "The secret word is:\n"<< soFar << endl;
    
    //Getting the players guess. Here is where the function should start
    
    char guess;
    cout<<"Guess a leter: ";
    cin>>guess;
    
    while ( used.find(guess) != string::npos )
    {
    
    cout<<"Guess a leter: ";
    cin>>guess;
    
    }//while 2
    
    used += guess;
    
    if ( THE_WORD.find(guess) != string::npos )
    {
    cout<<"Letter "<<guess<<" is in the word."<< endl;
    for ( int i = 0 ; i < THE_WORD.length() ; ++i )
    if ( THE_WORD[i] == guess )
    soFar[i] = guess;
    }
    else
    {
    cout<<"Letter "<<guess<<" is not part of the secret word."<<endl;
    ++wrong;
    }
    }//This is the end of the get the guess part
    //Ending the game
    if ( wrong == MAX_WRONG)
    cout<<"You have been hanged!";
    else
    cout<<"Bravo! You have guessed the full secret word\n";
    
    return 0;
    }//main
    but it does not conclude all features!!!

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Hmm, I can find the above code in many places all over the internet.

    You still don't seem to be getting it. We don't just write code for you, you haven't even asked a question, and your code has next to no indentation. We can tell you how to do small specific parts that you can demonstrate some attempt at writing yourself (not copying other's code), or we can re-explain things at a high level to you.

    So you want a hint?: Learn about proper code indentation and apply that knowledge here.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Drop the course would be my suggestion.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Salem View Post
    Drop the course would be my suggestion.
    ^this
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman Help - Code included
    By darren78 in forum C Programming
    Replies: 3
    Last Post: 02-17-2009, 08:35 AM
  2. Hangman Game Troubles
    By emerica240 in forum C Programming
    Replies: 9
    Last Post: 11-26-2008, 12:39 AM
  3. Hangman, Help me with the thinking
    By Livijn in forum C# Programming
    Replies: 14
    Last Post: 02-09-2008, 02:16 PM
  4. Help doing Hangman...
    By Kreative in forum C Programming
    Replies: 11
    Last Post: 08-18-2002, 09:22 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21