Answer to Question #51091 in C++ for Zexy

Question #51091
In Chapter 5, you created a game named GuessAWord that is similar to Hangman and in which a player guesses letters to try to replicate a hidden word. (You also made modifica- tions to the game in Chapter 12.) Modify the game so that a user can store 25 words in a text file, any of which might become the randomly-selected word to guess during a game. You can use the Words.txt file in the Chapter13 folder of the data files provided with your book, or you can create your own. Save the file as GuessAWordFileInput.cpp.
1
Expert's answer
2015-03-04T02:42:39-0500
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <fstream> // for file
#include <time.h> // for generating random numbers
using namespace std;
const string FILE_NAME = "words.txt";
int main(){
string allWords[25];
int index = 0;
int randIndex = 0;
//These variables are used to read from file
char str_term[256];

//Reading file:
ifstream fin(FILE_NAME); // open file for reading
// if the file 'input' is missing then display error message
if (!fin.is_open())
{
cout<<"Error!!Input file cannot be found !!"<<endl;
}
//if the file 'input' is found
else
{
cout<<"Available words for answer: "<<endl;
while(!fin.eof())
{
fin.getline(str_term, 256); // read line from file
if (index < 25){
allWords[index] = &str;_term[0];
++index;
}
}
for (int i = 0; i < index; i++)
cout<<allWords[i]<<endl;
// for generating random numbers
srand(time(NULL));
randIndex = rand()%;
string answer;
cout<<"Enter your answer : "<<answer<<endl;
//clear input
cin.clear();
cin.sync();
cin>>answer;
if (answer == allWords[randIndex]) cout<<"You win!!!"<<endl;
else
cout<<"You lose!!!"<<endl;
}
system&#40;"pause"&#41;;
return 0;
}
words.txt
word
vfdg
ertyr
qwde
hgjghj
qwerwer
zxdc
fg
erter
gf
wefef
szdfcsdf
sdf
yhty
asfd
jrthjy
sdfd
ertergt
dsfsdf
erwe
sdfsd
wefrw
xdf
wefr
erwgt

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS
paypal