I need to modify this because is not letting the user input the letters. Run it
ID: 3620868 • Letter: I
Question
I need to modify this because is not letting the user input the letters. Run it and Please let me know what I'm missing. Also dont forget to create a notepad file. Thanks______________________________________________________________________
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <time.h>
using namespace std;
#define MAX_GUESSES 5
#define MAX_INCORRECT_GUESSES 9
#define MAX_WORD_LENGTH 15
#define MAX_WORDS 50
int read_words(char[],char[][MAX_WORD_LENGTH]);
void get_word(char[][MAX_WORD_LENGTH],char[],int);
int get_random(int);
void print_guessed_letters(char[],int);
int good_guess(char[],char);
void printword(char[]);
char getletter();
int main()
{
int i,count,used=0,guesses=0,good,found=0;
char filename[20];
char WordArray[MAX_WORDS][MAX_WORD_LENGTH];
char Word[MAX_WORD_LENGTH];
char word_so_far[MAX_WORD_LENGTH];
char lettersUsed[26];
char again='y',guess;
cout<<"This is the game of Hangman ";
cout<<"I will choose a word, and you will enter characters to ";
cout<<"try to guess the characters in my word without making ";
cout<<"more than nine incorrect guesses. If you guess all the letter ";
cout<<"in my word before making nine incorrect guesses, you win! ";
cout<<"what is the name of the file you are using? ";
cin>>filename;
count=read_words(filename,WordArray);
if(count==-1)
return 1;
cout<<"Your file had "<<count<<" words ";
while(toupper(again)=='Y')
{
get_word(WordArray,Word,count);
//cout<<"Your *hangman secret word* is: "<<Word<<" ";
used=0;
guess=0;
found=0;
for(i=0;i<26;i++)
lettersUsed[i]=' ';
for(i=0;i<<strlen(Word);i++)
word_so_far[i]='-';
word_so_far[i]='';
cout<<" Your word ";
printword(word_so_far);
while(guesses<<MAX_INCORRECT_GUESSES&&found<<strlen(Word))
{guess=getletter();
lettersUsed[used++]=guess;
if(good_guess(Word,guess)==1)
{ for(i=0;i<<strlen(Word);i++)
if(Word[i]==guess)
{ word_so_far[i]=guess;
found++;
}
}
else
guesses++;
print_guessed_letters(lettersUsed,used);
cout<<"Your word so far ";
printword(word_so_far);
}
if(found==strlen(Word))
cout<<"YOU WON!! :) :) ";
else
cout<<"sorry :( :( The word was "<<Word<<endl<<endl;
cout<<"another game?(Y/N) ";
cin>>again;
}
cout<<"Hope you had fun! BYE for now ";
system("pause");
}
char getletter()
{char letter;
cout<<"Enter a new letter: ";
cin>>letter;
return toupper(letter);
}
void printword(char w[])
{int i;
for(i=0;i<<strlen(w);i++)
cout<<w[i];
cout<<" ";
}
int good_guess(char w[],char let)
{int i;
for(i=0;i<<strlen(w);i++)
if(let==w[i])
{cout<<"Good guess! My word contains your letter! ";
return 1;
}
cout<<"Sorry! That letter is not in the chosen word! ";
return 0;
}
void print_guessed_letters(char letters[],int n)
{int i;
cout<<"The letters you have guessed so far are: ";
for(i=0;i<n;i++)
cout<<letters[i]<<" ";
cout<<" ";
}
void get_word(char a[][MAX_WORD_LENGTH],char w[],int count)
{
int n,i;
n=get_random(count);
for(i=0;i<MAX_WORD_LENGTH;i++)
w[i]=toupper(a[n][i]);
return;
}
int get_random(int num_words)
{
srand(time(0)); //"seeds" the random number generator
return(rand() % num_words);
}
int read_words(char filename[],char a[][MAX_WORD_LENGTH])
{
int i,j,count=0,k;
ifstream input;
input.open(filename); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return 1;
}
input>>a[count];
while(input)
{count++;
input>>a[count];
}
input.close();
return count;
}
Explanation / Answer
please rate - thanks #include "stdafx.h"#include <iostream>
#include <cstring>
#include <cstdlib>
#include <fstream>
using namespace std;
#define MAX_GUESSES 5
#define MAX_INCORRECT_GUESSES 9
#define MAX_WORD_LENGTH 15
#define MAX_WORDS 50
int read_words(char[],char[][MAX_WORD_LENGTH]);
void get_word(char[][MAX_WORD_LENGTH],char[],int);
int get_random(int);
void print_guessed_letters(char[],int);
int good_guess(char[],char);
void printword(char[]);
char getletter();
int main()
{
int i,count,used=0,guesses=0,good,found=0;
char filename[20];
char WordArray[MAX_WORDS][MAX_WORD_LENGTH];
char Word[MAX_WORD_LENGTH];
char word_so_far[MAX_WORD_LENGTH];
char lettersUsed[26];
char again='y',guess;
cout<<"This is the game of Hangman ";
cout<<"I will choose a word, and you will enter characters to ";
cout<<"try to guess the characters in my word without making ";
cout<<"more than nine incorrect guesses. If you guess all the letter ";
cout<<"in my word before making nine incorrect guesses, you win! ";
cout<<"what is the name of the file you are using? ";
cin>>filename;
count=read_words(filename,WordArray);
if(count==-1)
return 1;
cout<<"Your file had "<<count<<" words ";
while(toupper(again)=='Y')
{
get_word(WordArray,Word,count);
// cout<<"Your *hangman secret word* is: "<<Word<<" ";
used=0;
guess=0;
found=0;
for(i=0;i<26;i++)
lettersUsed[i]=' ';
for(i=0;i<strlen(Word);i++)
word_so_far[i]='-';
word_so_far[i]='';
cout<<" Your word ";
printword(word_so_far);
while(guesses<MAX_INCORRECT_GUESSES&&found<strlen(Word))
{guess=getletter();
lettersUsed[used++]=guess;
if(good_guess(Word,guess)==1)
{ for(i=0;i<strlen(Word);i++)
if(Word[i]==guess)
{ word_so_far[i]=guess;
found++;
}
}
else
guesses++;
print_guessed_letters(lettersUsed,used);
cout<<"Your word so far ";
printword(word_so_far);
}
if(found==strlen(Word))
cout<<"YOU WON!! :) :) ";
else
cout<<"sorry :( :( The word was "<<Word<<endl<<endl;
cout<<"another game?(Y/N) ";
cin>>again;
}
cout<<"Hope you had fun! BYE for now ";
system("pause");
}
char getletter()
{char letter;
cout<<"Enter a new letter: ";
cin>>letter;
return toupper(letter);
}
void printword(char w[])
{int i;
for(i=0;i<strlen(w);i++)
cout<<w[i];
cout<<" ";
}
int good_guess(char w[],char let)
{int i;
for(i=0;i<strlen(w);i++)
if(let==w[i])
{cout<<"Good guess! My word contains your letter! ";
return 1;
}
cout<<"Sorry! That letter is not in the chosen word! ";
return 0;
}
void print_guessed_letters(char letters[],int n)
{int i;
cout<<"The letters you have guessed so far are: ";
for(i=0;i<n;i++)
cout<<letters[i]<<" ";
cout<<" ";
}
void get_word(char a[][MAX_WORD_LENGTH],char w[],int count)
{
int n,i;
n=get_random(count);
for(i=0;i<MAX_WORD_LENGTH;i++)
w[i]=toupper(a[n][i]);
return;
}
int get_random(int num_words)
{
srand(time(0)); //"seeds" the random number generator
return(rand() % num_words);
}
int read_words(char filename[],char a[][MAX_WORD_LENGTH])
{
int i,j,count=0,k;
ifstream input;
input.open(filename); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return 1;
}
input>>a[count];
while(input)
{count++;
input>>a[count];
}
input.close();
return count;
}