How to write and read from a sequential file into the game Hangman using fstream? Here are the instructions.
Functions enterNewWords() and readFromFile() [ correspond to the example code for writing to sequential files and reading from sequential files from the text book. ]. Instead of displaying the contents of the file, the function inserts the contents into the vector named wordlist. The readFromFile() also selects a random word from the vector wordlist which is stored in the variable secretWord.
Functions enterNewWords() and readFromFile() [ correspond to the example code for writing to sequential files and reading from sequential files from the text book. ]. Instead of displaying the contents of the file, the function inserts the contents into the vector named wordlist. The readFromFile() also selects a random word from the vector wordlist which is stored in the variable secretWord.
Code:
#ifndef HANGMAN_H
#define HANGMAN_H
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <fstream>
using namespace std;
class Hangman
{
private:
int attempts;
vector<string>wordList;
string secretWord;
int wordLength;
public:
Hangman();
void playGame();
void enterNewWords();
bool isWin(string word);
void convertToUpper();
void readFromFile();
void setSecretWord(string word);
string getSecretWord();
void setWordLength(int length);
int getWordLength();
void displayMenu();
};
#endif
Comment