Description
The purpose of this challenge is to read and write files. This challenge will very loosely simulate a word game (think Words With Friends or Scrabble).
Requirements
-
-
- Acquire the file called enable1.txt. This file contains all the words that are considered valid in Words With Friends
- Create a function bool find_word(string filename, string word). This function will open a file whose filename is passed in the filename parameter. This function will open and read the file one line at a time searching for the existence of word. If the entire file is read and the word is not found, return false. If the word is found in the file, return true. An efficient approach to writing this function would be to return a value of true as soon as the word is found and stop searching the rest of the file. Because filename is a string type, you will have to use the .c_str() function on filename when it’s passed to the open function call.
- Create a function bool is_word_valid(string word). This function will simply call the find_word() function in Step 2, passing the string literal “enable1.txt” as the filename parameter. Note that the is_word_valid() function has one parameter but the find_word() function has two parameters. This type of function is called a wrapper function; a function that simply calls another function with a little bit of functionality added.
- Create a function bool is_word_used(string word). This function will also call the find_word() function from Step 2. This time, the filename parameter for find_word() will be the string literal “word.uses”
- At this point, we’ve created the find_word() function as a generic function that allows us to search any file for any word, depending on which file is opened.
- Create a function void save_usage(string word). This function will call is_word_used() to see if the “word.uses” file already contains an entry for word. If the word is already in the file, do nothing. If the “word.uses” file doesn’t contain the word, then append/add the word to the file. Make sure to open the “word.uses” file in this function in append mode; this means that the file will not always be recreated when written to — new writes to the file will add to the end of the file. To open a file for append, write code as below:
ofstream fout; fout.open("word.uses", ios::app);
- In main(), ask the user to enter a word. The program will check to see if the word entered is valid using the is_word_valid() function. If the word entered by the user is not a valid word, report a message to the user. If the word is valid, see if the user has used this word before by calling the is_word_used() function. If so, report that the word has been previously used. Otherwise, report that the word has now been used. See interaction below.
-
Sample main()
int main() { string word; cout << "Play your word: "; cin >> word; if (is_word_valid(word)) { if (is_word_used(word)) { cout << "You've played " << word << " before " << endl; } else { cout << "This is your first time playing " << word << endl; } } else cout << "That is not a valid word" << endl; return 0; }
Sample Interaction
[Run your program] Play your word: abcde That is not a valid word [Run your program] Play your word: apple This is your first time playing apple [Run your program] Play your word: apple You've played apple before [Run your program] Play your word: juicy This is your first time playing juicy
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-CHAL0035
Print Requirements