We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,650 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

need help with hangman game

hi, this is my assignment and my code is below too. I don't know why it does not work.

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;



int main()
{
    int const size=20;
    char word[20];
    char dash[size]={"            "};
    char guess;
    int number = 6;
    int lives; 
    int ind; 


    // Open file

    ifstream myfile;
    myfile.open("test.txt");
    myfile >> word;

    // Create another text file 

    ofstream another;
    another.open("another.txt");
    for (int i=0; i<size && word[i]!=' '; i++){
        if(toupper(word[i])>='A' && toupper(word[i])<='Z'){
            dash[i]='-';
            another << i;
        }
    }


    another.close();

    for (number=6; number>0; number--){
        cout << "Word: ";
        for(int k=0; k<strlen(word); k++){
            cout<<dash[k];
        }
        cout << " "<<endl;
    }

    //Making another guess
    cout << "Guess your letter: \n";
    cin >> guess;
    cout << "\n";

    lives=0;

    //Open another.txt 
    ifstream anotherfile;
    anotherfile.open("another.txt");
    for (int j=0; j<strlen(word); j++){
        if (guess==word[j]){
            cout<<"Your guess was right. \n";
            anotherfile>>ind;
            word[j]=dash[ind];
            number=number++;
            lives=1;

    }
        anotherfile.close();}
    if (lives==0){
        cout << "Your guess was wrong. \n";
    }

    if (number>6){
        cout << "The word is "<< word << "\n";
    }else {
        cout<< "You won the game";
    }
    return 0;
}

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.

2
Contributors
1
Reply
14 Minutes
Discussion Span
4 Months Ago
Last Updated
5
Views
cucumalay
Newbie Poster
1 post since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

For this assignment your are to write a program, using strings (from the string class) that plays the popular game of Hangman.

Well you might want to switch over all of your char arrays to string like the instructions tell you to use. Might make the rest of the code a little easier.

NathanOliver
Posting Virtuoso
1,524 posts since Apr 2009
Reputation Points: 281
Solved Threads: 281
Skill Endorsements: 3

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1003 seconds using 2.9MB