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.