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("pause"); 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
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
Finding a professional expert in "partial differential equations" in the advanced level is difficult.
You can find this expert in "Assignmentexpert.com" with confidence.
Exceptional experts! I appreciate your help. God bless you!
Comments
Leave a comment